run 6.0 KB

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