1
0
mirror of https://github.com/rancher/os.git synced 2025-05-31 02:46:00 +00:00
os/scripts/run
2015-08-06 00:05:25 -07:00

142 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
set -e
set -x
cd $(dirname $0)/..
source scripts/build-common
BASE=$(pwd)
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
UNAME=$(uname)
USER_DATA=cloud-init/openstack/latest/user_data
# 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
;;
*)
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
cd ${INITRD_TMP}
find . | cpio -H newc -o > ${INITRD_TEST}
if [ ! -e ${HD} ]; then
mkdir -p $(dirname ${HD})
if [ "$UNAME" == "Darwin" ]; then qemu-img create -f raw -o size=10G ${HD}
elif [ "$UNAME" == "Linux" ]; then gzip -dc ${HD_GZ} > ${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="rancher.password=rancher rancher.modules=[9p,9pnet_virtio] console=ttyS0 ${QEMU_APPEND}"
if [ "$UNAME" == "Darwin" ] && [ -x $(which xhyve) ]; then
CLOUD_CONFIG_ISO="$(pwd)/cloud-config.iso"
rm -rf ${CLOUD_CONFIG_ISO}
# PREREQ: brew install cdrtools
mkisofs -R -V config-2 -o "${CLOUD_CONFIG_ISO}" "$(pwd)/cloud-init"
echo PWD=`pwd`
exec sudo 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 rancher.state.formatzero=true rancher.state.autoformat=[/dev/sda,/dev/vda] ${KERNEL_ARGS}"
elif [ "$UNAME" == "Linux" ] && [ -x $(which qemu-system-x86_64) ]; then
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} \
-machine accel=kvm \
-cpu host \
-smp 4 \
-fsdev local,id=conf,security_model=none,readonly,path=$(pwd)/cloud-init \
-device virtio-9p-pci,fsdev=conf,mount_tag=config-2 \
-append "${KERNEL_ARGS}" \
-serial mon:telnet:localhost:4444,server,nowait \
-nographic \
${QEMU_ARGS} "${@}"
else
exit 42
fi