run 11 KB

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