run 10 KB

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