#!/bin/bash -l # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # build script which wraps around packer and virtualbox to create the systemvm template function usage() { cat <&2 } function error() { log ERROR $@ exit 1 } # cleanup code support declare -a on_exit_items function on_exit() { for (( i=${#on_exit_items[@]}-1 ; i>=0 ; i-- )) ; do sleep 2 log DEBUG "on_exit: ${on_exit_items[i]}" eval ${on_exit_items[i]} done } function add_on_exit() { local n=${#on_exit_items[*]} on_exit_items[${n}]="$*" if [ ${n} -eq 0 ]; then log DEBUG "Setting trap" trap on_exit EXIT fi } # retry code support function retry() { local times=$1 shift local count=0 while [ ${count} -lt ${times} ]; do "$@" && break count=$(( $count + 1 )) sleep ${count} done if [ ${count} -eq ${times} ]; then error "Failed ${times} times: $@" fi } ### ### Script logic ### function create_definition() { if [ "${appliance}" != "${appliance_build_name}" ]; then cp -r "${appliance}" "${appliance_build_name}" set +e if [ ! -z "${version}" ]; then if [ -f "${appliance_build_name}/scripts/configure_systemvm_services.sh" ]; then sed ${sed_regex_option} -i -e "s/^CLOUDSTACK_RELEASE=.+/CLOUDSTACK_RELEASE=${version}/" \ "${appliance_build_name}/scripts/configure_systemvm_services.sh" fi fi set -e add_on_exit rm -rf "${appliance_build_name}" fi ./shar_cloud_scripts.sh add_on_exit rm -f cloud_scripts_shar_archive.sh } function prepare() { log INFO "preparing for build" rm -rf dist *.ova *.vhd *.vdi *.qcow* *.bz2 *.vmdk *.ovf } function packer_build() { log INFO "building new image with packer" cd ${appliance_build_name} && packer build template-base_${base_arch}-target_${target_arch}.json && cd .. } function stage_vmx() { cat << VMXFILE > "${1}.vmx" .encoding = "UTF-8" displayname = "${1}" annotation = "${1}" guestos = "otherlinux-64" virtualHW.version = "11" config.version = "8" numvcpus = "1" cpuid.coresPerSocket = "1" memsize = "256" pciBridge0.present = "TRUE" pciBridge4.present = "TRUE" pciBridge4.virtualDev = "pcieRootPort" pciBridge4.functions = "8" pciBridge5.present = "TRUE" pciBridge5.virtualDev = "pcieRootPort" pciBridge5.functions = "8" pciBridge6.present = "TRUE" pciBridge6.virtualDev = "pcieRootPort" pciBridge6.functions = "8" pciBridge7.present = "TRUE" pciBridge7.virtualDev = "pcieRootPort" pciBridge7.functions = "8" vmci0.present = "TRUE" floppy0.present = "FALSE" ide0:0.clientDevice = "FALSE" ide0:0.present = "TRUE" ide0:0.deviceType = "atapi-cdrom" ide0:0.autodetect = "TRUE" ide0:0.startConnected = "FALSE" mks.enable3d = "false" svga.autodetect = "false" svga.vramSize = "4194304" scsi0:0.present = "TRUE" scsi0:0.deviceType = "disk" scsi0:0.fileName = "$2" scsi0:0.mode = "persistent" scsi0:0.writeThrough = "false" scsi0.virtualDev = "lsilogic" scsi0.present = "TRUE" vmci0.unrestricted = "false" vcpu.hotadd = "false" vcpu.hotremove = "false" firmware = "bios" mem.hotadd = "false" VMXFILE } function xen_server_export() { log INFO "creating xen server export" set +e which faketime >/dev/null 2>&1 && which vhd-util >/dev/null 2>&1 local result=$? set -e if [ ${result} == 0 ]; then qemu-img convert -f qcow2 -O raw "dist/${appliance}" img.raw vhd-util convert -s 0 -t 1 -i img.raw -o stagefixed.vhd faketime '2010-01-01' vhd-util convert -s 1 -t 2 -i stagefixed.vhd -o "${appliance_build_name}-xen.vhd" rm -f *.bak bzip2 "${appliance_build_name}-xen.vhd" mv "${appliance_build_name}-xen.vhd.bz2" dist/ log INFO "${appliance} exported for XenServer: dist/${appliance_build_name}-xen.vhd.bz2" else log WARN "** Skipping ${appliance_build_name} export for XenServer: faketime or vhd-util command is missing. **" log WARN "** faketime source code is available from https://github.com/wolfcw/libfaketime **" fi } function ovm_export() { log INFO "creating OVM export" qemu-img convert -f qcow2 -O raw "dist/${appliance}" "dist/${appliance_build_name}-ovm.raw" cd dist && bzip2 "${appliance_build_name}-ovm.raw" && cd .. log INFO "${appliance} exported for OracleVM: dist/${appliance_build_name}-ovm.raw.bz2" } function kvm_export() { log INFO "creating kvm export" set +e qemu-img convert -o compat=0.10 -f qcow2 -c -O qcow2 "dist/${appliance}" "dist/${appliance_build_name}-kvm.qcow2" local qemuresult=$? cd dist && bzip2 "${appliance_build_name}-kvm.qcow2" && cd .. log INFO "${appliance} exported for KVM: dist/${appliance_build_name}-kvm.qcow2.bz2" } function vmware_export() { log INFO "creating vmware export" qemu-img convert -f qcow2 -O vmdk "dist/${appliance}" "dist/${appliance_build_name}-vmware.vmdk" if ! ovftool_loc="$(type -p "ovftool")" || [ -z "$ovftool_loc" ]; then log INFO "ovftool not found, skipping ova generation for VMware" return fi log INFO "ovftool found, using it to export ova file" CDIR=$PWD cd dist chmod 666 ${appliance_build_name}-vmware.vmdk stage_vmx ${appliance_build_name}-vmware ${appliance_build_name}-vmware.vmdk ovftool ${appliance_build_name}-vmware.vmx ${appliance_build_name}-vmware.ova rm -f *vmx *vmdk cd $CDIR log INFO "${appliance} exported for VMWare: dist/${appliance_build_name}-vmware.ova" } function hyperv_export() { log INFO "creating hyperv export" qemu-img convert -f qcow2 -O vpc "dist/${appliance}" "dist/${appliance_build_name}-hyperv.vhd" CDIR=$PWD cd dist zip "${appliance_build_name}-hyperv.vhd.zip" "${appliance_build_name}-hyperv.vhd" rm -f *vhd cd $CDIR log INFO "${appliance} exported for HyperV: dist/${appliance_build_name}-hyperv.vhd.zip" } ### ### Main invocation ### function main() { prepare create_definition packer_build # process the disk at dist kvm_export if [ "${target_arch}" == "x86_64" ]; then ovm_export xen_server_export vmware_export hyperv_export fi rm -f "dist/${appliance}" cd dist && chmod +r * && cd .. cd dist && md5sum * > md5sum.txt && cd .. cd dist && sha512sum * > sha512sum.txt && cd .. add_on_exit log INFO "BUILD SUCCESSFUL" } # we only run main() if not source-d return 2>/dev/null || main