run 11 KB

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