fix(image_to_vm): Add VM MAC address to Vagrantfile, create box image.

Vagrant should now be good to go!
This commit is contained in:
Michael Marineau 2013-08-01 19:39:12 -04:00
parent 626ebd7eb0
commit ae3202da32
2 changed files with 53 additions and 24 deletions

View File

@ -9,6 +9,8 @@ SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
DEFINE_string vm_name "CoreOS" "Name for this VM"
DEFINE_string disk_vmdk "" "Disk image to reference, only basename is used."
DEFINE_integer memory_size 1024 "Memory size in MB"
DEFINE_string output_ovf "" "Path to write ofv file to, required."
DEFINE_string output_vagrant "" "Path to write Vagrantfile to, optional."
# Parse command line
FLAGS "$@" || exit 1
@ -22,6 +24,11 @@ if [[ ! -e "${FLAGS_disk_vmdk}" ]]; then
exit 1
fi
if [[ -z "${FLAGS_output_ovf}" ]]; then
echo "No ovf file path provided." >&2
exit 1
fi
DISK_NAME=$(basename "${FLAGS_disk_vmdk}")
DISK_UUID=$(uuidgen)
DISK_SIZE_BYTES=$(qemu-img info -f vmdk "${FLAGS_disk_vmdk}" \
@ -39,12 +46,29 @@ macgen() {
hexdump -n3 -e "\"${VBOX_MAC_PREFIX}%X\n\"" /dev/urandom
}
# Date format as used in this file
# Used in both the ovf and Vagrantfile
PRIMARY_MAC=$(macgen)
# Date format as used in ovf
datez() {
date -u "+%Y-%m-%dT%H:%M:%SZ"
}
cat <<EOF
if [[ -n "${FLAGS_output_vagrant}" ]]; then
cat >"${FLAGS_output_vagrant}" <<EOF
Vagrant.configure("2") do |config|
config.vm.base_mac = "${PRIMARY_MAC}"
# SSH in as the default 'core' user, it has the vagrant ssh key.
config.ssh.username = "core"
# Disable the base shared folder, guest additions are unavailable.
config.vm.synced_folder ".", "/vagrant", disabled: true
end
EOF
fi
cat >"${FLAGS_output_ovf}" <<EOF
<?xml version="1.0"?>
<Envelope ovf:version="1.0" xml:lang="en-US" xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vbox="http://www.virtualbox.org/ovf/machine">
<References>
@ -164,7 +188,7 @@ cat <<EOF
</BIOS>
<USBController enabled="false" enabledEhci="false"/>
<Network>
<Adapter slot="0" enabled="true" MACAddress="$(macgen)" cable="true" speed="0" type="82540EM">
<Adapter slot="0" enabled="true" MACAddress="${PRIMARY_MAC}" cable="true" speed="0" type="82540EM">
<DisabledModes/>
<NAT>
<DNS pass-domain="true" use-proxy="false" use-host-resolver="false"/>

View File

@ -409,7 +409,7 @@ _write_ovf_conf() {
--vm_name "$VM_NAME" \
--disk_vmdk "$VM_DST_IMG" \
--memory_size "$vm_mem" \
> "$ovf"
--output_ovf "$ovf"
local ovf_name=$(basename "${ovf}")
cat > "${VM_README}" <<EOF
@ -420,40 +420,45 @@ EOF
VM_GENERATED_FILES+=( "$ovf" "${VM_README}" )
}
vm_cleanup() {
info "Cleaning up temporary files"
rm -rf "${VM_TMP_DIR}"
}
_write_vagrant_conf() {
local vm_mem="${1:-$(_get_vm_opt MEM)}"
local src_name=$(basename "$VM_SRC_IMG")
local dst_name=$(basename "$VM_DST_IMG")
local dst_dir=$(dirname "$VM_DST_IMG")
local ovf="${dst_dir}/$(_src_to_dst_name "${src_name}" ".ovf")"
local vfile="${dst_dir}/$(_src_to_dst_name "${src_name}" ".Vagrantfile")"
local ovf="${VM_TMP_DIR}/box.ovf"
local vfile="${VM_TMP_DIR}/Vagrantfile"
local box="${dst_dir}/$(_src_to_dst_name "${src_name}" ".box")"
# Move the disk image to tmp, it won't be a final output file
mv "${VM_DST_IMG}" "${VM_TMP_DIR}/${dst_name}"
"${BUILD_LIBRARY_DIR}/virtualbox_ovf.sh" \
--vm_name "$VM_NAME" \
--disk_vmdk "$VM_DST_IMG" \
--disk_vmdk "${VM_TMP_DIR}/${dst_name}" \
--memory_size "$vm_mem" \
> "$ovf"
--output_ovf "$ovf" \
--output_vagrant "$vfile"
cat > "${vfile}" <<EOF
Vagrant.configure("2") do |config|
# SSH in as the default 'core' user, it has the vagrant ssh key.
config.ssh.username = "core"
# Disable the base shared folder, guest additions are unavailable.
config.vm.synced_folder ".", "/vagrant", disabled: true
end
EOF
tar -cf "${box}" -C "${VM_TMP_DIR}" "box.ovf" "Vagrantfile" "${dst_name}"
cat > "${VM_README}" <<EOF
Vagrant >= 1.2 is required.
Vagrant >= 1.2 is required. Use something like the following to get started:
vagrant box add coreos path/to/$(basename "${box}")
vagrant init coreos
vagrant up
vagrant ssh
You will get a warning about "No guest additions were detected...",
this is expected and should be ignored. SSH should work just dandy.
EOF
VM_GENERATED_FILES+=( "$ovf" "${vfile}" "${VM_README}" )
# Replace list, not append, since we packaged up the disk image.
VM_GENERATED_FILES=( "${box}" "${VM_README}" )
}
vm_cleanup() {
info "Cleaning up temporary files"
rm -rf "${VM_TMP_DIR}"
}
print_readme() {