run 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/bin/bash
  2. set -e
  3. # help: build and run RancherOS, requires KVM local
  4. cd $(dirname $0)/..
  5. HOST_ARCH=${HOST_ARCH:-amd64}
  6. ARCH=${ARCH:-amd64}
  7. declare -A qemuarch=( ["amd64"]="x86_64" ["arm"]="arm" ["arm64"]="aarch64" )
  8. declare -A ttycons=( ["amd64"]="ttyS0" ["arm"]="ttyAMA0" ["arm64"]="ttyAMA0" )
  9. declare -A machine=( ["amd64"]="" ["arm"]="-M virt" ["arm64"]="-M virt" )
  10. declare -A cpu=( ["amd64"]="" ["arm"]="-cpu cortex-a8" ["arm64"]="-cpu cortex-a57" )
  11. declare -A network=(
  12. ["amd64"]="-net nic,vlan=0,model=virtio -net user,vlan=0,hostfwd=tcp::2222-:22,hostname=rancher-dev"
  13. ["arm"]="-netdev user,id=unet,hostfwd=tcp::2222-:22,hostname=rancher-dev -device virtio-net-device,netdev=unet"
  14. )
  15. network["arm64"]=${network["arm"]}
  16. hd_amd64() {
  17. echo "-drive if=virtio,file=$1"
  18. }
  19. hd_arm() {
  20. echo "-drive if=none,id=hd0,file=$1 -device virtio-blk-device,drive=hd0"
  21. }
  22. declare -A hd=( ["amd64"]="hd_amd64" ["arm"]="hd_arm" ["arm64"]="hd_arm" )
  23. cd_amd64() {
  24. echo "-hdc $1"
  25. }
  26. cd_arm() {
  27. echo "-drive if=none,id=cd0,file=$1 -device virtio-blk-device,drive=cd0"
  28. }
  29. declare -A cd=( ["amd64"]="cd_amd64" ["arm"]="cd_arm" ["arm64"]="cd_arm" )
  30. BUILD=build
  31. BASE=$(pwd)
  32. UNAME=$(uname)
  33. KERNEL=${BASE}/dist/artifacts/vmlinuz
  34. INITRD_SRC=${BASE}/build/initrd
  35. INITRD=${BASE}/build/initrd.tmp
  36. QEMU=1
  37. FORMAT=1
  38. RM_USR=1
  39. REBUILD=1
  40. while [ "$#" -gt 0 ]; do
  41. case $1 in
  42. --append)
  43. shift 1
  44. QEMU_APPEND="${QEMU_APPEND} $1"
  45. ;;
  46. --name)
  47. shift 1
  48. NAME="$1"
  49. ;;
  50. --cloud-config)
  51. shift 1
  52. CLOUD_CONFIG="$1" || :
  53. if [ ! -f ${CLOUD_CONFIG} ]; then
  54. echo No such file: "'"${CLOUD_CONFIG}"'" 1>&2
  55. exit 1
  56. fi
  57. ;;
  58. --arch)
  59. shift 1
  60. ARCH="$1"
  61. ;;
  62. --qemu)
  63. QEMU=1
  64. ;;
  65. --qind)
  66. QIND=1
  67. QEMU=0
  68. ;;
  69. --kvm)
  70. KVM=1
  71. ;;
  72. --no-format)
  73. FORMAT=0
  74. ;;
  75. --no-rm-usr)
  76. RM_USR=0
  77. ;;
  78. --no-rebuild)
  79. REBUILD=0
  80. ;;
  81. --fresh)
  82. FRESH=1
  83. ;;
  84. *)
  85. break
  86. ;;
  87. esac
  88. shift 1
  89. done
  90. QEMUARCH=${qemuarch["${ARCH}"]}
  91. TTYCONS=${ttycons["${ARCH}"]}
  92. if [ "$QEMU" == "1" ] && ! which qemu-system-${QEMUARCH}; then
  93. QEMU=0
  94. fi
  95. if [[ ! -e ${KERNEL} || ! -e ${INITRD_SRC} ]]; then
  96. echo "Failed to find ${KERNEL} or ${INITRD_SRC}" 1>&2
  97. exit 1
  98. fi
  99. if [ "$REBUILD" == "1" ] || [ ! -e ${INITRD} ]; then
  100. cp bin/ros ${INITRD_SRC}/usr/bin/ros
  101. ./scripts/hash-initrd
  102. pushd ${INITRD_SRC} >/dev/null
  103. find . | cpio -H newc -o | gzip -1 > ${INITRD}
  104. popd >/dev/null
  105. fi
  106. KERNEL_ARGS="quiet rancher.password=rancher console=${TTYCONS} ${QEMU_APPEND}"
  107. if [ "$FORMAT" == "1" ]; then
  108. KERNEL_ARGS="${KERNEL_ARGS} rancher.state.formatzero=true rancher.state.autoformat=[/dev/sda,/dev/vda]"
  109. fi
  110. if [ "$RM_USR" == "1" ]; then
  111. KERNEL_ARGS="${KERNEL_ARGS} rancher.rm_usr"
  112. fi
  113. if [ "$KVM" == "" ] && [ -c /dev/kvm ] && [ -r /dev/kvm ] && [ -w /dev/kvm ]; then
  114. KVM=1
  115. fi
  116. if [ "$QEMU" == "1" ]; then
  117. HD=${BASE}/state/hd.img
  118. [ "$FRESH" == "1" ] && rm -f ${HD} >/dev/null 2>&1 || :
  119. if [ ! -e ${HD} ]; then
  120. mkdir -p $(dirname ${HD})
  121. qemu-img create -f qcow2 -o size=10G ${HD}
  122. fi
  123. CCROOT=${BUILD}/cloud-config
  124. rm -rf ${CCROOT}
  125. mkdir -p ${CCROOT}
  126. USER_DATA=${CCROOT}/openstack/latest/user_data
  127. mkdir -p $(dirname ${USER_DATA})
  128. rm -f ${USER_DATA} || :
  129. if [ -n "$CLOUD_CONFIG" ]; then
  130. cat ${CLOUD_CONFIG} > ${USER_DATA}
  131. else
  132. echo "#cloud-config" > ${USER_DATA}
  133. echo "ssh_authorized_keys:" >> ${USER_DATA}
  134. echo "- $(<${BASE}/assets/rancher.key.pub)" >> ${USER_DATA}
  135. fi
  136. CPU=${cpu["$ARCH"]}
  137. if [ "$KVM" == "1" ] && [ "$ARCH" == "$HOST_ARCH" ]; then
  138. KVM_ENABLE="-enable-kvm"
  139. CPU="-cpu host"
  140. fi
  141. set -x
  142. HOME=${HOME:-/}
  143. exec qemu-system-${QEMUARCH} -serial stdio \
  144. -rtc base=utc,clock=host \
  145. ${KVM_ENABLE} \
  146. ${CPU} \
  147. ${machine["$ARCH"]} \
  148. -kernel ${KERNEL} \
  149. -initrd ${INITRD} \
  150. -m 2048 \
  151. ${network["$ARCH"]} \
  152. $(eval "${hd["$ARCH"]} ${HD}") \
  153. -smp 1 \
  154. -append "${KERNEL_ARGS}" \
  155. -nographic \
  156. -display none \
  157. -fsdev local,security_model=passthrough,readonly,id=fsdev0,path=${CCROOT} \
  158. -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2 \
  159. -fsdev local,security_model=none,id=fsdev1,path=${HOME} \
  160. -device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag=home \
  161. ${QEMU_ARGS} \
  162. "${@}"
  163. elif [ "$QIND" == "1" ]; then
  164. NAME=${NAME:-ros-qind}
  165. if [ "$FRESH" == "1" ]; then
  166. docker rm -fv ${NAME} >/dev/null 2>&1 || :
  167. fi
  168. if ! docker inspect ${NAME} >/dev/null 2>&1; then
  169. if [ "$KVM" == "1" ]; then
  170. KVM_ENABLE="--device=/dev/kvm:/dev/kvm"
  171. fi
  172. mkdir -p ./tmp
  173. TMP=$(mktemp -d ./tmp/ros-qind-XXXXXX)
  174. mkdir -p ${TMP}/stuff
  175. trap "rm -rf ${TMP}" EXIT
  176. ln ${CLOUD_CONFIG} ${KERNEL} ${INITRD} ./assets/rancher.key ./assets/rancher.key.pub ${TMP}/stuff/
  177. KERNEL_FILE=$(basename ${KERNEL})
  178. INITRD_FILE=$(basename ${INITRD})
  179. if [ -n "$CLOUD_CONFIG" ]; then
  180. CLOUD_CONFIG_ENABLE="--cloud-config /stuff/$(basename ${CLOUD_CONFIG})"
  181. else
  182. SSH_PUB_ENABLE="--ssh-pub /stuff/rancher.key.pub"
  183. fi
  184. set -x
  185. docker create --name=${NAME} -it ${KVM_ENABLE} \
  186. rancher/qind \
  187. --hostname "rancher-dev" \
  188. ${CLOUD_CONFIG_ENABLE} \
  189. ${SSH_PUB_ENABLE} \
  190. -m 1G -kernel /stuff/${KERNEL_FILE} -initrd /stuff/${INITRD_FILE} -append "${KERNEL_ARGS}" \
  191. "${@}"
  192. docker cp ${TMP}/stuff ${NAME}:/stuff
  193. rm -rf ${TMP}
  194. fi
  195. exec docker start -ai ${NAME}
  196. else
  197. exit 42
  198. fi