run 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. MEMORY=2048
  7. while [ "$#" -gt 0 ]; do
  8. case $1 in
  9. --iso)
  10. BOOT_ISO=1
  11. QEMU=0
  12. QIND=0
  13. REBUILD=0
  14. ;;
  15. --boothd)
  16. BOOT_HD=1
  17. QEMU=0
  18. QIND=0
  19. REBUILD=0
  20. ;;
  21. --append)
  22. shift 1
  23. QEMU_APPEND="${QEMU_APPEND} $1"
  24. ;;
  25. --memory)
  26. shift 1
  27. MEMORY="$1"
  28. ;;
  29. --name)
  30. shift 1
  31. NAME="$1"
  32. ;;
  33. --cloud-config)
  34. shift 1
  35. CLOUD_CONFIG="$1" || :
  36. if [ ! -f ${CLOUD_CONFIG} ]; then
  37. echo No such file: "'"${CLOUD_CONFIG}"'" 1>&2
  38. exit 1
  39. fi
  40. ;;
  41. --cloud-config-iso)
  42. CLOUD_CONFIG_FORMAT="iso"
  43. ;;
  44. --cloud-config-fat)
  45. CLOUD_CONFIG_FORMAT="fat"
  46. ;;
  47. --second-drive)
  48. SECOND_DRIVE=1
  49. ;;
  50. --arch)
  51. shift 1
  52. ARCH="$1"
  53. ;;
  54. --qemu)
  55. QEMU=1
  56. ;;
  57. --qind)
  58. QIND=1
  59. QEMU=0
  60. ;;
  61. --kvm)
  62. KVM=1
  63. ;;
  64. --no-format)
  65. FORMAT=0
  66. ;;
  67. --no-rm-usr)
  68. RM_USR=0
  69. ;;
  70. --no-rebuild)
  71. REBUILD=0
  72. ;;
  73. --fresh)
  74. FRESH=1
  75. ;;
  76. --b2d)
  77. # use together with --fresh to trigger boot2docker/machine auto-format
  78. AUTOFORMAT=1
  79. ;;
  80. --console)
  81. # use the bios console, not serial (lets you see syslinux)
  82. CONSOLEDISPLAY=1
  83. ;;
  84. --installed)
  85. ./scripts/create-installed
  86. INSTALLED=1
  87. ;;
  88. *)
  89. break
  90. ;;
  91. esac
  92. shift 1
  93. done
  94. if [ "$QEMU" == "1" ] && ! which qemu-system-${QEMUARCH}; then
  95. QEMU=0
  96. fi
  97. if [[ ! -e ${KERNEL} || ! -e ${INITRD_SRC} ]]; then
  98. echo "Failed to find ${KERNEL} or ${INITRD_SRC}" 1>&2
  99. exit 1
  100. fi
  101. echo "initrd ${INITRD}"
  102. if [ "$REBUILD" == "1" ] || [ ! -e ${INITRD} ]; then
  103. echo "rebuilding ${INITRD} from ${INITRD_SRC}"
  104. cp bin/ros ${INITRD_SRC}/usr/bin/ros
  105. ./scripts/hash-initrd
  106. pushd ${INITRD_SRC} >/dev/null
  107. find . | cpio -H newc -o | gzip -1 > ${INITRD}
  108. popd >/dev/null
  109. fi
  110. KERNEL_ARGS="${DEFAULT_KERNEL_ARGS} ${QEMU_APPEND}"
  111. if [ "$FORMAT" == "1" ]; then
  112. KERNEL_ARGS="${KERNEL_ARGS} rancher.state.dev=LABEL=RANCHER_STATE rancher.state.autoformat=[/dev/sda,/dev/vda]"
  113. fi
  114. if [ "$RM_USR" == "1" ]; then
  115. KERNEL_ARGS="${KERNEL_ARGS} rancher.rm_usr"
  116. fi
  117. if [ "$KVM" == "" ] && [ -c /dev/kvm ] && [ -r /dev/kvm ] && [ -w /dev/kvm ]; then
  118. KVM=1
  119. fi
  120. set -x
  121. if [ "$QEMU" == "1" ] || [ "$BOOT_ISO" == "1" ] || [ "$BOOT_HD" == "1" ]; then
  122. HD=${BASE}/state/hd.img
  123. HD2=${BASE}/state/hd2.img
  124. [ "$FRESH" == "1" ] && rm -f ${HD} ${HD2} >/dev/null 2>&1 || :
  125. if [ ! -e ${HD} ]; then
  126. mkdir -p $(dirname ${HD})
  127. if [ ¨$INSTALLED¨ == ¨1¨ ]; then
  128. ./scripts/create-installed
  129. else
  130. if [ "$AUTOFORMAT" == "1" ]; then
  131. echo "boot2docker, please format-me" | cat - /dev/zero | head -c 5242880 > format-flag.txt # 5M
  132. qemu-img convert -f raw format-flag.txt -O qcow2 ${HD}
  133. qemu-img resize ${HD} +10GB
  134. else
  135. qemu-img create -f qcow2 -o size=10G ${HD}
  136. fi
  137. fi
  138. fi
  139. if [ "$SECOND_DRIVE" == "1" ]; then
  140. if [ "$FRESH" == "1" ]; then
  141. qemu-img create -f qcow2 -o size=10G ${HD2}
  142. fi
  143. SECOND_DRIVE_ENABLE=$(eval "${hd["$ARCH"]} ${HD2}")
  144. fi
  145. fi
  146. if [ "$QIND" != "1" ]; then
  147. CPU=${cpu["$ARCH"]}
  148. if [ "$KVM" == "1" ] && [ "$ARCH" == "$HOST_ARCH" ]; then
  149. KVM_ENABLE="-enable-kvm"
  150. CPU="-cpu host"
  151. fi
  152. CCROOT=${BUILD}/cloud-config
  153. rm -rf ${CCROOT}
  154. mkdir -p ${CCROOT}
  155. USER_DATA=${CCROOT}/openstack/latest/user_data
  156. mkdir -p $(dirname ${USER_DATA})
  157. rm -f ${USER_DATA} || :
  158. if [ -n "$CLOUD_CONFIG" ]; then
  159. cat ${CLOUD_CONFIG} > ${USER_DATA}
  160. else
  161. echo "#cloud-config" > ${USER_DATA}
  162. echo "ssh_authorized_keys:" >> ${USER_DATA}
  163. echo "- $(<${BASE}/assets/rancher.key.pub)" >> ${USER_DATA}
  164. fi
  165. case $CLOUD_CONFIG_FORMAT in
  166. iso)
  167. mkisofs -R -V config-2 -o ${BASE}/state/configdrive.iso ${CCROOT}
  168. CLOUD_CONFIG_DISK="-cdrom ${BASE}/state/configdrive.iso"
  169. ;;
  170. fat)
  171. echo "TODO: implement a vfat formated qemu img & copy the config files into it"
  172. exit 1
  173. ;;
  174. *)
  175. CLOUD_CONFIG_DISK="-fsdev local,security_model=passthrough,readonly,id=fsdev0,path=${CCROOT} \
  176. -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2"
  177. ;;
  178. esac
  179. HOME=${HOME:-/}
  180. fi
  181. if [ "$CONSOLEDISPLAY" == "1" ]; then
  182. DISPLAY_OPTS="-curses"
  183. else
  184. # default
  185. DISPLAY_OPTS="-nographic -serial stdio -display none"
  186. fi
  187. if [ "$QEMU" == "1" ]; then
  188. if [ "$INSTALLED" == "1" ]; then
  189. # kernel args only works when using -kernel
  190. KERNEL_ARGS=""
  191. else
  192. INSTALLED_ARGS="-kernel ${KERNEL} -initrd ${INITRD}"
  193. fi
  194. set -x
  195. exec qemu-system-${QEMUARCH} \
  196. ${DISPLAY_OPTS} \
  197. -rtc base=utc,clock=host \
  198. ${INSTALLED_ARGS} \
  199. -append "${KERNEL_ARGS}" \
  200. ${KVM_ENABLE} \
  201. ${CPU} \
  202. ${machine["$ARCH"]} \
  203. -m $MEMORY \
  204. ${network["$ARCH"]} \
  205. $(eval "${hd["$ARCH"]} ${HD}") \
  206. ${SECOND_DRIVE_ENABLE} \
  207. -smp 1 \
  208. ${CLOUD_CONFIG_DISK} \
  209. -fsdev local,security_model=none,id=fsdev1,path=${HOME} \
  210. -device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag=home \
  211. ${QEMU_ARGS} \
  212. "${@}"
  213. elif [ "$BOOT_ISO" == "1" ] ||
  214. [ "$BOOT_HD" == "1" ]; then
  215. if [ "$BOOT_ISO" == "1" ]; then
  216. ISO_OPTS="-boot d -cdrom ./dist/artifacts/rancheros.iso \
  217. ${CLOUD_CONFIG_DISK} \
  218. -fsdev local,security_model=none,id=fsdev1,path=${HOME} \
  219. -device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag=home "
  220. echo "----- $ISO_OPTS"
  221. fi
  222. set -x
  223. exec qemu-system-${QEMUARCH} \
  224. ${DISPLAY_OPTS} \
  225. -rtc base=utc,clock=host \
  226. ${KVM_ENABLE} \
  227. ${CPU} \
  228. ${machine["$ARCH"]} \
  229. -m $MEMORY \
  230. ${network["$ARCH"]} \
  231. $(eval "${hd["$ARCH"]} ${HD}") \
  232. ${SECOND_DRIVE_ENABLE} \
  233. -smp 1 \
  234. ${ISO_OPTS}
  235. elif [ "$QIND" == "1" ]; then
  236. NAME=${NAME:-ros-qind}
  237. if [ "$FRESH" == "1" ]; then
  238. docker rm -fv ${NAME} >/dev/null 2>&1 || :
  239. fi
  240. if ! docker inspect ${NAME} >/dev/null 2>&1; then
  241. if [ "$KVM" == "1" ]; then
  242. KVM_ENABLE="--device=/dev/kvm:/dev/kvm"
  243. fi
  244. mkdir -p ./tmp
  245. TMP=$(mktemp -d ./tmp/ros-qind-XXXXXX)
  246. mkdir -p ${TMP}/stuff
  247. trap "rm -rf ${TMP}" EXIT
  248. ln ${CLOUD_CONFIG} ${KERNEL} ${INITRD} ./assets/rancher.key ./assets/rancher.key.pub ${TMP}/stuff/
  249. KERNEL_FILE=$(basename ${KERNEL})
  250. INITRD_FILE=$(basename ${INITRD})
  251. if [ -n "$CLOUD_CONFIG" ]; then
  252. CLOUD_CONFIG_ENABLE="--cloud-config /stuff/$(basename ${CLOUD_CONFIG})"
  253. else
  254. SSH_PUB_ENABLE="--ssh-pub /stuff/rancher.key.pub"
  255. fi
  256. set -x
  257. docker create --name=${NAME} -it ${KVM_ENABLE} \
  258. rancher/qind \
  259. --hostname "rancher-dev" \
  260. ${CLOUD_CONFIG_ENABLE} \
  261. ${SSH_PUB_ENABLE} \
  262. -m 1G -kernel /stuff/${KERNEL_FILE} -initrd /stuff/${INITRD_FILE} -append "${KERNEL_ARGS}" \
  263. "${@}"
  264. docker cp ${TMP}/stuff ${NAME}:/stuff
  265. rm -rf ${TMP}
  266. fi
  267. exec docker start -ai ${NAME}
  268. else
  269. exit 42
  270. fi