#!/bin/bash set -e set -x cd $(dirname $0)/.. source scripts/build-common BASE=$(pwd) UNAME=$(uname) # Linux and Darwin SHA1 sum binary are different, pick which to use if [ "$UNAME" == "Darwin" ]; then sha1sum=$(which shasum) elif [ "$UNAME" == "Linux" ]; then sha1sum=$(which sha1sum); fi KERNEL=${BASE}/dist/artifacts/vmlinuz INITRD=${BASE}/dist/artifacts/initrd NO_COMPRESS_INITRD=${INITRD}.none HD=${BASE}/state/empty-hd.img HD_GZ=${BASE}/assets/empty-hd.img.gz INITRD_TMP=${BUILD}/$(${sha1sum} ${INITRD} | awk '{print $1}') INITRD_CURRENT=${BUILD}/initrd-current INITRD_TEST=${BUILD}/initrd.test USER_DATA=${BUILD}/cloud-config/openstack/latest/user_data FORMAT=1 # PREREQ: brew install coreutils path() { if [ "$UNAME" == "Darwin" ]; then greadlink -f "$1" elif [ "$UNAME" == "Linux" ]; then readlink -f "$1"; fi } while [ "$#" -gt 0 ]; do case $1 in --append) shift 1 QEMU_APPEND="${QEMU_APPEND} $1" ;; --cloud-config) shift 1 CLOUD_CONFIG=$(path "$1") || : if [ ! -f ${CLOUD_CONFIG} ]; then echo No such file: "'"${CLOUD_CONFIG}"'" 1>&2 exit 1 fi ;; --xhyve) if [ "$UNAME" == "Darwin" ] && [ -x $(which xhyve) ]; then XHYVE=1 fi ;; --no-format) FORMAT=0 ;; *) break ;; esac shift 1 done if [[ ! -e ${KERNEL} || ! -e ${INITRD} ]]; then echo "Failed to find ${KERNEL} or ${INITRD}" 1>&2 exit 1 fi if [ ! -d ${INITRD_TMP} ]; then mkdir -p ${INITRD_TMP} pushd ${INITRD_TMP} if [ -e ${NO_COMPRESS_INITRD} ]; then cat ${NO_COMPRESS_INITRD} | cpio -idmv else xz -dc ${INITRD} | cpio -idmv fi popd fi if [ -e ${INITRD_CURRENT} ]; then rm -f ${INITRD_CURRENT} fi ln -sf ${INITRD_TMP} ${INITRD_CURRENT} mkdir -p ${INITRD_TMP}/usr/{bin,share/ros} cp bin/rancheros ${INITRD_TMP}/usr/bin/ros cp -f os-config.yml ${INITRD_TMP}/usr/share/ros pushd ${INITRD_TMP} find . | cpio -H newc -o > ${INITRD_TEST} popd if [ ! -e ${HD} ]; then mkdir -p $(dirname ${HD}) if [ "$XHYVE" == "1" ]; then qemu-img create -f raw -o size=10G ${HD} else qemu-img create -f qcow2 -o size=10G ${HD} fi fi mkdir -p $(dirname $USER_DATA) if [ -n "$CLOUD_CONFIG" ]; then cat ${CLOUD_CONFIG} > ${USER_DATA} else echo "#cloud-config" > ${USER_DATA} echo "ssh_authorized_keys:" >> ${USER_DATA} echo " - $(<${BASE}/assets/rancher.key.pub)" >> ${USER_DATA} for i in ${HOME}/.ssh/*.pub; do if [ -e $i ]; then echo " - $(<$i)" >> ${USER_DATA} fi done fi KERNEL_ARGS="quiet rancher.password=rancher console=ttyS0 ${QEMU_APPEND}" if [ "$FORMAT" == "1" ]; then KERNEL_ARGS="${KERNEL_ARGS} rancher.state.formatzero=true rancher.state.autoformat=[/dev/sda,/dev/vda]" fi CLOUD_CONFIG_ISO="${BUILD}/cloud-config.iso" rm -rf ${CLOUD_CONFIG_ISO} mkisofs -R -V config-2 -o "${CLOUD_CONFIG_ISO}" "$BUILD/cloud-config" if [ "$XHYVE" == "1" ]; then echo PWD=`pwd` exec sudo -n xhyve -A -H -P -u \ -m 4G \ -s 0:0,hostbridge -s 31,lpc \ -l com1,stdio \ -s 2:0,virtio-net \ -s 3,ahci-cd,${CLOUD_CONFIG_ISO} \ -s 4,virtio-blk,${HD} \ -U a01fb25c-3a19-4759-a47a-2e353e51807d \ -f kexec,${KERNEL},${INITRD_TEST},"earlyprintk=serial elevator=noop ${KERNEL_ARGS}" elif [ -x $(which qemu-system-x86_64) ]; then if [ -c /dev/kvm ] && [ -r /dev/kvm ] && [ -w /dev/kvm ]; then KVM_ENABLE="-machine accel=kvm -cpu host" fi exec qemu-system-x86_64 -serial stdio \ -kernel ${KERNEL} \ -initrd ${INITRD_TEST} \ -m 1024 \ -net nic,vlan=0,model=virtio \ -net user,vlan=0,hostfwd=tcp::2222-:22,hostname=rancher-dev \ -drive if=virtio,file=${HD} \ ${KVM_ENABLE} \ -smp 4 \ -cdrom ${CLOUD_CONFIG_ISO} \ -append "${KERNEL_ARGS}" \ -nographic \ -display none \ ${QEMU_ARGS} "${@}" else exit 42 fi