feat(vm_image_util): Rework the qemu wrapper script to work for PXE

Now the script can be used with a disk image or a kernel/initrd.
Using a disk with the PXE kernel should work too but haven't tried it.
This commit is contained in:
Michael Marineau 2014-03-22 22:28:10 -07:00
parent e151a33cee
commit 066bd23df8
2 changed files with 50 additions and 21 deletions

View File

@ -4,15 +4,17 @@ SCRIPT_DIR="`dirname "$0"`"
VM_NAME=
VM_UUID=
VM_IMAGE=
VM_KERNEL=
VM_INITRD=
VM_MEMORY=
VM_NCPUS="`grep -c ^processor /proc/cpuinfo`"
IMAGE_PATH="${SCRIPT_DIR}/${VM_IMAGE}"
SSH_PORT=2222
SSH_KEYS=""
USAGE="Usage: $0 [-a authorized_keys] [--] [qemu options...]
Options:
-a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub]
-p PORT The port on localhost to map to the VM's sshd. [2222]
-s Safe settings: single simple cpu, ide disks.
-h this ;-)
This script is a wrapper around qemu for starting CoreOS virtual machines.
@ -26,12 +28,14 @@ Any arguments after -a and -p will be passed through to qemu, -- may be
used as an explicit separator. See the qemu(1) man page for more details.
"
safe_args=0
script_args=1
while getopts ":a:p:vh" OPTION
while getopts ":a:p:svh" OPTION
do
case $OPTION in
a) SSH_KEYS="$OPTARG" ;;
p) SSH_PORT="$OPTARG" ;;
s) safe_args=1 ;;
v) set -x ;;
h) echo "$USAGE"; exit ;;
?) break ;;
@ -73,18 +77,35 @@ else
done
fi
# Start assembling our default command line arguments
if [ "${safe_args}" -eq 1 ]; then
disk_type="ide"
else
disk_type="virtio"
# Emulate the host CPU closely in both features and cores.
set -- -cpu host -smp "${VM_NCPUS}" "$@"
fi
if [ -n "${VM_IMAGE}" ]; then
set -- -drive if=${disk_type},file="${SCRIPT_DIR}/${VM_IMAGE}" "$@"
fi
if [ -n "${VM_KERNEL}" ]; then
set -- -kernel "${SCRIPT_DIR}/${VM_KERNEL}" "$@"
fi
if [ -n "${VM_INITRD}" ]; then
set -- -initrd "${SCRIPT_DIR}/${VM_INITRD}" "$@"
fi
# Default to KVM, fall back on full emulation
# Emulate the host CPU closely in both features and cores.
# ${METADATA} will be mounted in CoreOS as /media/metadata
qemu-system-x86_64 \
-name "$VM_NAME" \
-uuid "$VM_UUID" \
-m ${VM_MEMORY} \
-cpu host \
-smp "${VM_NCPUS}" \
-machine accel=kvm:tcg \
-drive index=0,if=virtio,media=disk,format=qcow2,file="${IMAGE_PATH}" \
-net nic,vlan=0,model=virtio \
-net user,vlan=0,hostfwd=tcp::"${SSH_PORT}"-:22 \
-fsdev local,id=metadata,security_model=none,readonly,path="${METADATA}" \
@ -96,4 +117,4 @@ RET=$?
# Cleanup!
rm -rf "${METADATA}"
trap - EXIT
exit $?
exit ${RET}

View File

@ -329,16 +329,12 @@ write_vm_conf() {
fi
}
_write_qemu_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 script="${dst_dir}/$(_src_to_dst_name "${src_name}" ".sh")"
_write_qemu_common() {
local script="$1"
local vm_mem="$(_get_vm_opt MEM)"
sed -e "s%^VM_NAME=.*%VM_NAME='${VM_NAME}'%" \
-e "s%^VM_UUID=.*%VM_UUID='${VM_UUID}'%" \
-e "s%^VM_IMAGE=.*%VM_IMAGE='${dst_name}'%" \
-e "s%^VM_MEMORY=.*%VM_MEMORY='${vm_mem}'%" \
"${BUILD_LIBRARY_DIR}/qemu_template.sh" > "${script}"
checkbashisms --posix "${script}" || die
@ -363,19 +359,31 @@ EOF
VM_GENERATED_FILES+=( "${script}" "${VM_README}" )
}
_write_pxe_conf() {
_write_qemu_conf() {
local script="$(_dst_dir)/$(_dst_name ".sh")"
local dst_name=$(basename "$VM_DST_IMG")
_write_qemu_common "${script}"
sed -e "s%^VM_IMAGE=.*%VM_IMAGE='${dst_name}'%" -i "${script}"
}
_write_pxe_conf() {
local script="$(_dst_dir)/$(_dst_name ".sh")"
local vmlinuz_name="$(_dst_name ".vmlinuz")"
local dst_name=$(basename "$VM_DST_IMG")
cat >"${VM_README}" <<EOF
If you have qemu installed (or in the SDK), you can start the image with:
cd path/to/image
_write_qemu_common "${script}"
sed -e "s%^VM_KERNEL=.*%VM_KERNEL='${vmlinuz_name}'%" \
-e "s%^VM_INITRD=.*%VM_INITRD='${dst_name}'%" -i "${script}"
qemu-kvm -m 1024 -kernel ${vmlinuz_name} -initrd ${dst_name} -append 'state=tmpfs: root=squashfs: sshkey="PUT AN SSH KEY HERE"'
cat >>"${VM_README}" <<EOF
You can pass extra kernel parameters with -append, for example:
./$(basename "${script}") -curses -append 'sshkey="PUT AN SSH KEY HERE"'
When using -nograhic or -serial you must also enable the serial console:
./$(basename "${script}") -nographic -append 'console=ttyS0,115200n8'
EOF
VM_GENERATED_FILES+=( "${VM_README}" )
}
# Generate the vmware config file