run 9.7 KB

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