123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #!/bin/bash
- set -e
- # help: build and run RancherOS, requires KVM local
- cd $(dirname $0)/..
- HOST_ARCH=${HOST_ARCH:-amd64}
- ARCH=${ARCH:-amd64}
- declare -A qemuarch=( ["amd64"]="x86_64" ["arm"]="arm" ["arm64"]="aarch64" )
- declare -A ttycons=( ["amd64"]="ttyS0" ["arm"]="ttyAMA0" ["arm64"]="ttyAMA0" )
- declare -A machine=( ["amd64"]="" ["arm"]="-M virt" ["arm64"]="-M virt" )
- declare -A cpu=( ["amd64"]="" ["arm"]="-cpu cortex-a8" ["arm64"]="-cpu cortex-a57" )
- declare -A network=(
- ["amd64"]="-net nic,vlan=0,model=virtio -net user,vlan=0,hostfwd=tcp::2222-:22,hostname=rancher-dev"
- ["arm"]="-netdev user,id=unet,hostfwd=tcp::2222-:22,hostname=rancher-dev -device virtio-net-device,netdev=unet"
- )
- network["arm64"]=${network["arm"]}
- hd_amd64() {
- echo "-drive if=virtio,file=$1"
- }
- hd_arm() {
- echo "-drive if=none,id=hd0,file=$1 -device virtio-blk-device,drive=hd0"
- }
- declare -A hd=( ["amd64"]="hd_amd64" ["arm"]="hd_arm" ["arm64"]="hd_arm" )
- cd_amd64() {
- echo "-hdc $1"
- }
- cd_arm() {
- echo "-drive if=none,id=cd0,file=$1 -device virtio-blk-device,drive=cd0"
- }
- declare -A cd=( ["amd64"]="cd_amd64" ["arm"]="cd_arm" ["arm64"]="cd_arm" )
- BUILD=build
- BASE=$(pwd)
- UNAME=$(uname)
- KERNEL=${BASE}/dist/artifacts/vmlinuz
- INITRD_SRC=${BASE}/build/initrd
- INITRD=${BASE}/build/initrd.tmp
- QEMU=1
- FORMAT=1
- RM_USR=1
- REBUILD=1
- while [ "$#" -gt 0 ]; do
- case $1 in
- --append)
- shift 1
- QEMU_APPEND="${QEMU_APPEND} $1"
- ;;
- --name)
- shift 1
- NAME="$1"
- ;;
- --cloud-config)
- shift 1
- CLOUD_CONFIG="$1" || :
- if [ ! -f ${CLOUD_CONFIG} ]; then
- echo No such file: "'"${CLOUD_CONFIG}"'" 1>&2
- exit 1
- fi
- ;;
- --arch)
- shift 1
- ARCH="$1"
- ;;
- --qemu)
- QEMU=1
- ;;
- --qind)
- QIND=1
- QEMU=0
- ;;
- --kvm)
- KVM=1
- ;;
- --no-format)
- FORMAT=0
- ;;
- --no-rm-usr)
- RM_USR=0
- ;;
- --no-rebuild)
- REBUILD=0
- ;;
- --fresh)
- FRESH=1
- ;;
- *)
- break
- ;;
- esac
- shift 1
- done
- QEMUARCH=${qemuarch["${ARCH}"]}
- TTYCONS=${ttycons["${ARCH}"]}
- if [ "$QEMU" == "1" ] && ! which qemu-system-${QEMUARCH}; then
- QEMU=0
- fi
- if [[ ! -e ${KERNEL} || ! -e ${INITRD_SRC} ]]; then
- echo "Failed to find ${KERNEL} or ${INITRD_SRC}" 1>&2
- exit 1
- fi
- if [ "$REBUILD" == "1" ] || [ ! -e ${INITRD} ]; then
- cp bin/ros ${INITRD_SRC}/usr/bin/ros
- ./scripts/hash-initrd
- pushd ${INITRD_SRC} >/dev/null
- find . | cpio -H newc -o | gzip -1 > ${INITRD}
- popd >/dev/null
- fi
- KERNEL_ARGS="quiet rancher.password=rancher console=${TTYCONS} ${QEMU_APPEND}"
- if [ "$FORMAT" == "1" ]; then
- KERNEL_ARGS="${KERNEL_ARGS} rancher.state.formatzero=true rancher.state.autoformat=[/dev/sda,/dev/vda]"
- fi
- if [ "$RM_USR" == "1" ]; then
- KERNEL_ARGS="${KERNEL_ARGS} rancher.rm_usr"
- fi
- if [ "$KVM" == "" ] && [ -c /dev/kvm ] && [ -r /dev/kvm ] && [ -w /dev/kvm ]; then
- KVM=1
- fi
- if [ "$QEMU" == "1" ]; then
- HD=${BASE}/state/hd.img
- [ "$FRESH" == "1" ] && rm -f ${HD} >/dev/null 2>&1 || :
- if [ ! -e ${HD} ]; then
- mkdir -p $(dirname ${HD})
- qemu-img create -f qcow2 -o size=10G ${HD}
- fi
- CCROOT=${BUILD}/cloud-config
- rm -rf ${CCROOT}
- mkdir -p ${CCROOT}
- USER_DATA=${CCROOT}/openstack/latest/user_data
- mkdir -p $(dirname ${USER_DATA})
- rm -f ${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}
- fi
- CPU=${cpu["$ARCH"]}
- if [ "$KVM" == "1" ] && [ "$ARCH" == "$HOST_ARCH" ]; then
- KVM_ENABLE="-enable-kvm"
- CPU="-cpu host"
- fi
- set -x
- HOME=${HOME:-/}
- exec qemu-system-${QEMUARCH} -serial stdio \
- -rtc base=utc,clock=host \
- ${KVM_ENABLE} \
- ${CPU} \
- ${machine["$ARCH"]} \
- -kernel ${KERNEL} \
- -initrd ${INITRD} \
- -m 2048 \
- ${network["$ARCH"]} \
- $(eval "${hd["$ARCH"]} ${HD}") \
- -smp 1 \
- -append "${KERNEL_ARGS}" \
- -nographic \
- -display none \
- -fsdev local,security_model=passthrough,readonly,id=fsdev0,path=${CCROOT} \
- -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2 \
- -fsdev local,security_model=none,id=fsdev1,path=${HOME} \
- -device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag=home \
- ${QEMU_ARGS} \
- "${@}"
- elif [ "$QIND" == "1" ]; then
- NAME=${NAME:-ros-qind}
- if [ "$FRESH" == "1" ]; then
- docker rm -fv ${NAME} >/dev/null 2>&1 || :
- fi
- if ! docker inspect ${NAME} >/dev/null 2>&1; then
- if [ "$KVM" == "1" ]; then
- KVM_ENABLE="--device=/dev/kvm:/dev/kvm"
- fi
- mkdir -p ./tmp
- TMP=$(mktemp -d ./tmp/ros-qind-XXXXXX)
- mkdir -p ${TMP}/stuff
- trap "rm -rf ${TMP}" EXIT
- ln ${CLOUD_CONFIG} ${KERNEL} ${INITRD} ./assets/rancher.key ./assets/rancher.key.pub ${TMP}/stuff/
- KERNEL_FILE=$(basename ${KERNEL})
- INITRD_FILE=$(basename ${INITRD})
- if [ -n "$CLOUD_CONFIG" ]; then
- CLOUD_CONFIG_ENABLE="--cloud-config /stuff/$(basename ${CLOUD_CONFIG})"
- else
- SSH_PUB_ENABLE="--ssh-pub /stuff/rancher.key.pub"
- fi
- set -x
- docker create --name=${NAME} -it ${KVM_ENABLE} \
- rancher/qind \
- --hostname "rancher-dev" \
- ${CLOUD_CONFIG_ENABLE} \
- ${SSH_PUB_ENABLE} \
- -m 1G -kernel /stuff/${KERNEL_FILE} -initrd /stuff/${INITRD_FILE} -append "${KERNEL_ARGS}" \
- "${@}"
- docker cp ${TMP}/stuff ${NAME}:/stuff
- rm -rf ${TMP}
- fi
- exec docker start -ai ${NAME}
- else
- exit 42
- fi
|