From 48dde09aa754cfaab6ac506937838afbb1dd518a Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Wed, 23 Mar 2016 18:21:55 -0700 Subject: Convert the repo to a source tree --- .../qemu-init-scripts/files/qemu-conf.example | 149 ------------ .../qemu-init-scripts/files/qemu-init-script | 262 -------------------- .../qemu-init-scripts/files/qtap-manipulate | 128 ---------- qemu-init-scripts/qemu-conf.example | 149 ++++++++++++ qemu-init-scripts/qemu-init-script | 263 +++++++++++++++++++++ qemu-init-scripts/qtap-manipulate | 128 ++++++++++ 6 files changed, 540 insertions(+), 539 deletions(-) delete mode 100644 app-emulation/qemu-init-scripts/files/qemu-conf.example delete mode 100755 app-emulation/qemu-init-scripts/files/qemu-init-script delete mode 100755 app-emulation/qemu-init-scripts/files/qtap-manipulate create mode 100644 qemu-init-scripts/qemu-conf.example create mode 100755 qemu-init-scripts/qemu-init-script create mode 100755 qemu-init-scripts/qtap-manipulate diff --git a/app-emulation/qemu-init-scripts/files/qemu-conf.example b/app-emulation/qemu-init-scripts/files/qemu-conf.example deleted file mode 100644 index 4911597..0000000 --- a/app-emulation/qemu-init-scripts/files/qemu-conf.example +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright 1999-2013 Gentoo Foundation -# Distributed under the terms of the GNU General Public License v2 - -# VERSION: 0.3.0 - -# DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD! - -# VMTYPE is auto determined via the file prefix -# ie: kvm.myserver would yield VMTYPE=kvm -# the two types allowed are: qemu, kvm - -# Machine type -# -# The specific qemu binary type -# eg. for VMTYPE=kvm, then /usr/bin/qemu-system-${QEMU_TYPE} -# eg. for VMTYPE=qemu, then /usr/bin/qemu-${QEMU_TYPE} -# QEMU_TYPE="x86_64" -QEMU_TYPE="x86_64" - -## Network settings - -# MAC address -# -# You can generate a new one with this command: -# -# echo "52:54:00:$(dd if=/dev/urandom bs=512 count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\).*$/\1:\2:\3/')" -# -MACADDR=52:54:00:10:34:35 - -# NIC type. -# -# Allowed: -# nat - Automatic qemu NAT network. -# br - Bridged network. Requires 'net.br0' service up and running. -# none - No network. -# -# Defaults to 'nat'. -# -#NIC_TYPE=br - -# NIC model. -# -# See qemu documentation. -# -# Defaults to 'virtio'. -# -#NIC_MODEL= - - -## Storage settings - -# Disk image -DISKIMAGE=/mnt/kvm/gentoo64.qcow2 - -# Drive model. -# -# See qemu documentation. -# -# Defaults to 'virtio'. -# -#DRIVE_MODEL= - -# Drive format. -# -# See qemu documentation. -# -# Defaults to autodetection. -# -#DRIVE_FORMAT= - -# Drive cache type. -# -# See qemu documentation. -# -# Note for zfs filesystems -# ensure you have set zfs -# # zfs set primarycache=all ${tank} -# set DRIVE_CACHE=writeback -# -# Defaults to none. -# -#DRIVE_CACHE= - -## General settings - -# The machine type to use -# see qemu help -# default is Standard PC (i440FX + PIIX, 1996) -# -#MACHINE="q35" - -# Enable/diasble kvm -# -# null value == disabled -# non null value == enabled -# -ENABLE_KVM=1 - -# cpu option -# see qemu help -# -#CPU=host - -# RAM memory allocated for the virtual machine -# -# Defaults to 512M -# -#MEMORY=1G - -# Processor cores allocated for the virtual machine. -# -# Defaults to 1 core -# -#SMP=2 - -# Foreground settings -# -# Allowed: -# vnc=:24 - Runs VNC at :24 -# sdl=:0 - Runs SDL in X.org server running at :0 -# none - Disables foreground -# -# Defaults to 'none'. -# -FOREGROUND="vnc=:24" - -# Low privilege user that runs the virtual machine. -# -# Defaults to 'nobody'. -#DROP_USER= - -# Timeout - How many seconds to wait before trying to hard-kill the virtual -# machine. -# -# Defaults to 300 seconds. -# -#TIMEOUT=60 - -# Any other qemu options you may want to use -# live cd example -OTHER_ARGS="-boot order=cd -cdrom /mnt/archive/gentoo.iso" - -# live-usb image install example, with optional boot menu, adjust the format type as needed -# Note, initial run with an empty primary drive, initial boot fails, -# reboot it (send Cntl-Alt-delete), use the F12 option and select the second drive, after installation -# the primary drive should boot automatically -#OTHER_ARGS="-drive if=virtio,file=/path/to/usb.img,format=raw,cache=writeback,media=disk -boot menu=on" - - diff --git a/app-emulation/qemu-init-scripts/files/qemu-init-script b/app-emulation/qemu-init-scripts/files/qemu-init-script deleted file mode 100755 index 937ddfe..0000000 --- a/app-emulation/qemu-init-scripts/files/qemu-init-script +++ /dev/null @@ -1,262 +0,0 @@ -#!/sbin/runscript -# Copyright 2009-2013 Gentoo Foundation -# Distributed under the terms of the GNU General Public License v2 - -# You are not supposed to run this script directly. Create a symlink -# for the qemu instance you want to run as well as a copy of the -# configuration file and modify it appropriately like so... -# -# ln -s qemu /etc/init.d/kvm.myserver -# cp /etc/conf.d/qemu /etc/conf.d/kvm.myserver -# nano /etc/conf.d/kvm.myserver -# -# where 'kvm' is the virtual machine type ('qemu' and 'kvm' allowed) -# and 'myserver' is the name of your instance. - -VERSION="0.3.0" - -VMNAME=${SVCNAME#*.} -VMTYPE=${SVCNAME%.*} -VM_BINARY= -PIDFILE=/run/vm/${SVCNAME}.pid -MONITOR=/run/vm/${SVCNAME}.monitor -QTAP_FILE=/run/vm/${SVCNAME}.qtap -ENABLE_SDL= - -discern_vm_binary() { - case "$VMTYPE" in - kvm) - VM_BINARY=$(which "qemu-system-${QEMU_TYPE}") - [ -z "$VM_BINARY" ] && eerror "Failed to find the binary for qemu-system-${QEMU_TYPE}" - ;; - qemu) - VM_BINARY=$(which "qemu-${QEMU_TYPE}") - [ -z "$VM_BINARY" ] && eerror "Failed to find the binary for qemu-${QEMU_TYPE}" - ;; - *) - eerror "Failed to discern the binary for $VMTYPE" - ;; - esac - return 0 -} - -discern_foreground() { - if [ -n "$VNC" ]; then - ewarn "VNC option is going away; set FOREGROUND=\"vnc=${VNC}\" instead" - FOREGROUND="vnc=${VNC}" - fi - case "${FOREGROUND:-none}" in - vnc*) - VNC=${FOREGROUND#*=} - if [ -z "$VNC" ]; then - eerror "FOREGROUND vnc is incorrectly set; must specify an address (:1 for example)" - return 1 - fi - ;; - sdl*) - ENABLE_SDL=${FOREGROUND#*=} - if [ -z "${ENABLE_SDL}" ]; then - eerror "FOREGROUND sdl is incorrectly set; must specify a DISPLAY address" - return 1 - fi - ;; - none) - ;; - *) - eerror "Unknown FOREGROUND setting: $FOREGROUND" - return 1 - ;; - esac - return 0 -} - -DROP_USER=${DROP_USER:-nobody} -MEMORY=${MEMORY:-512M} -TIMEOUT=${TIMEOUT:-300} -SMP=${SMP:-1} -export KVM_USER=${KVM_USER:-"root"} - -extra_commands="reboot version" - -depend() { - if [ "$VMNAME" = "$SVCNAME" ]; then - return 0 - fi - - sanity_check || return 1 - - case "$NIC_TYPE" in - br) - need ${NIC_NET_DEPS-net.br0} - ;; - esac -} - -send_command() { - local command="socat -u - UNIX-CONNECT:${MONITOR}" - type -p nc6 > /dev/null && command="nc6 -U ${MONITOR} --send-only" - echo "$@" | ${command} > /dev/null 2>&1 -} - -symlink_check() { - if [ "${VMNAME}" = "${SVCNAME}" ]; then - eerror "You are not supposed to run this script directly. Create a symlink" - eerror "for the qemu instance you want to run as well as a copy of the" - eerror "configuration file and modify it appropriately like so..." - eerror - eerror " ln -s qemu /etc/init.d/kvm.myserver" - eerror " cp /etc/conf.d/qemu /etc/conf.d/kvm.myserver" - eerror " `basename "${EDITOR:-nano}"` /etc/conf.d/kvm.myserver" - eerror - eerror "where 'kvm' is the virtual machine type ('qemu' and 'kvm' allowed)" - eerror "and 'myserver' is the name of your instance." - return 1 - fi -} - -sanity_check() { - symlink_check || return 1 - - DISKIMAGE=$(readlink -f "${DISKIMAGE}") - if [ ! -f "${DISKIMAGE}" -a ! -b "${DISKIMAGE}" ]; then - eerror "couldn't find \$DISKIMAGE '$DISKIMAGE'" - return 1; - fi - discern_vm_binary || return 1 - NIC_TYPE=${NIC_TYPE:-nat} - - discern_foreground || return 1 -} - -start_pre() { - checkpath -d --owner root:root --mode 0644 "${PIDFILE%/*}" \ - "${MONITOR%/*}" -} - -start() { - sanity_check || return 1 - - local NIC_COMMAND=( -net "nic,model=${NIC_MODEL:-virtio},macaddr=${MACADDR}" -net ) - - if [ "${NIC_TYPE}" = "br" ]; then - ebegin "creating qtap ${QTAP:-(auto allocating one)}" - if [ -n "$QTAP" ]; then - qtap-manipulate create_specific "${QTAP}" -u "${DROP_USER}" - else - QTAP=$(qtap-manipulate create -u "${DROP_USER}") - if [ 0 != $? ]; then - eerror "failed to create qtap interface" - return 1 - fi - fi - echo "${QTAP}" > ${QTAP_FILE} - eend $? - NIC_COMMAND[${#NIC_COMMAND[@]}]="tap,ifname=${QTAP},script=no" - elif [ "${NIC_TYPE}" = "none" ]; then - NIC_COMMAND=( -net none ) - else - NIC_COMMAND[${#NIC_COMMAND[@]}]=user - fi - - local ss_args=( ) - local vm_args=( -daemonize ) - - if [ -n "${ENABLE_SDL}" ]; then - ss_args[${#ss_args[@]}]=--env - ss_args[${#ss_args[@]}]="DISPLAY=${ENABLE_SDL}" - ss_args[${#ss_args[@]}]=--env - local user_home=`getent passwd ${DROP_USER:-root} | cut -d: -f6` - ss_args[${#ss_args[@]}]="XAUTHORITY=$user_home/.Xauthority" - vm_args[${#vm_args[@]}]=-display - vm_args[${#vm_args[@]}]=sdl - elif [ -n "$VNC" ]; then - vm_args[${#vm_args[@]}]=-display - vm_args[${#vm_args[@]}]="vnc=${VNC}" - else - vm_args[${#vm_args[@]}]=-display - vm_args[${#vm_args[@]}]=none - fi - - ebegin "Starting ${VM_BINARY##*/} for ${VMNAME} ${FOREGROUND:+at ${FOREGROUND}}" - set -- start-stop-daemon \ - --start "${VM_BINARY}" \ - --pidfile ${PIDFILE} \ - "${ss_args[@]}" \ - -- \ - "${vm_args[@]}" \ - -pidfile ${PIDFILE} -monitor unix:${MONITOR},server,nowait \ - -runas ${DROP_USER} -name ${VMNAME} \ - -drive file="${DISKIMAGE//,/,,}",if=${DRIVE_MODEL:-virtio},cache=${DRIVE_CACHE:-none}${DRIVE_FORMAT:+,format=${DRIVE_FORMAT}} \ - "${NIC_COMMAND[@]}" \ - ${ENABLE_KVM:+--enable-kvm} ${CPU:+-cpu ${CPU}} \ - ${MACHINE:+-M ${MACHINE}} \ - ${MEMORY:+-m ${MEMORY}} ${SMP:+-smp ${SMP}} \ - ${OTHER_ARGS} - einfo "invoking ${@}" - "${@}" - ret=$? - if [ "0" != "${ret}" -a -n "${QTAP}" ]; then - qtap-manipulate destroy ${QTAP} - fi - - eend ${ret} -} - -reboot() { - symlink_check || return 1 - - ebegin "Rebooting ${VMNAME}" - send_command system_reset - eend $? -} - -stop() { - sanity_check || return 1 - - ebegin "Powering off ${VMNAME}" - send_command system_powerdown - eend $? - - ebegin "waiting up to ${TIMEOUT} seconds for it to die" - local pid - [ -s "${PIDFILE}" ] && pid=$(cat "${PIDFILE}") - if [ -z "$pid" ]; then - eerror "Couldn't find stored pid at '$PIDFILE'; user will have to manually kill kvm" - eerror "Will attempt to destroy qtap despite." - eend 1 - else - local ret=1 - for x in $(seq 0 ${TIMEOUT}); do - if kill -0 "${pid}" > /dev/null 2>&1; then - sleep 1s - continue - fi - ret=0 - break - done - eend $ret - fi - - ebegin "Stopping ${VM_BINARY##*/} for ${VMNAME}" - if kill -0 "${pid}" > /dev/null 2>&1; then - start-stop-daemon --stop "${VM_BINARY}" \ - --user "${DROP_USER}" \ - --pidfile "${PIDFILE}" \ - --quiet - eend $? - else - eend 0 # no need to kill process if it is dead :P - fi - local qtap - [ -s "${QTAP_FILE}" ] && qtap=$(cat "${QTAP_FILE}") - if [ -n "$qtap" ]; then - ebegin "destroying qtap ${qtap}" - qtap-manipulate destroy ${qtap} - eend $? - fi -} - -version() { - echo "qemu-init-scripts version: ${VERSION}" -} - diff --git a/app-emulation/qemu-init-scripts/files/qtap-manipulate b/app-emulation/qemu-init-scripts/files/qtap-manipulate deleted file mode 100755 index 53ab346..0000000 --- a/app-emulation/qemu-init-scripts/files/qtap-manipulate +++ /dev/null @@ -1,128 +0,0 @@ -#!/bin/sh -# Copyright 1999-2012 Gentoo Foundation -# Distributed under the terms of the GNU General Public Licens - -VERSION="0.3.0" - -has() { - local desired=$1 x - shift - for x in "$@"; do - [ "$desired" = "$x" ] && return 0; - done - return 1 -} - -find_available_node() { - local val=$(ip addr | grep -i '.*: qtap' | cut -d: -f2) - local pos=0 - while has qtap${pos} ${val##*()}; do - pos=$(( $pos + 1 )) - done - echo qtap${pos} -} - -create_node() { - local qtap="$1" - shift - tunctl -b -t "${qtap}" "$@" > /dev/null || die "tunctl failed" - brctl addif br0 "${qtap}" || die "brctl failed" - ip link set "${qtap}" up || die "ip link set ${qtap} failed" - ip link set "${qtap}" promisc on || die "ip link set ${qtap} promiscuos mode failed" -} - -destroy_node() { - issue= - ip link set ${1} down || { echo "ip link set ${1} down failed";issue=1; } - brctl delif br0 ${1} || { echo "brctl failed";issue=2; } - tunctl -d ${1} > /dev/null || { echo "tunctl failed";issue=3;} - [ -n "${issue}" ] && exit $(( $issue )) -} - -die() { - echo "$@" >&2 - exit 1 -} - -usage() { - echo "commands available:" - echo "create-specific qtap-name [ -u user ] [ -g group ]" - echo "create [ -u user ] [ -g group ]" - echo "destroy qtap-name" - echo "version" - echo -} - -usage_die() { - usage - die "$@" -} - -show_version() { - echo "qtap-manipulate version: ${VERSION}" -} - -create_user= -create_group= - -parse_create_options() { - while [ $# -ne 0 ]; do - local x="$1" - case "$x" in - -u=*) - shift - set -- "-u" "${x#-u=}" "$@" - ;& - -u) - shift - [ -z "$1" ] && die "-u requires an argument" - create_user="$1" - shift - ;; - -g=*) - shift - set -- "-g" "${x#-u=}" "$@" - ;& - -g) - shift - [ -z "$1" ] && die "-g requires an argument" - create_group="$2" - shift - ;; - *) - die "unknown option $1" - esac - done -} - -output_qtap=false -case "$1" in - destroy) - shift - [ $# -eq 0 ] && usage_die "destroy requires a second argument" - [ $# -gt 1 ] && usage_die "no idea what to do with args: $@" - destroy_node "$1" - ;; - create) - output_qtap=true - qtap=$(find_available_node) - [ -z "$qtap" ] && die "failed to find a qtap node to use" - shift - set -- create_specific "${qtap}" "$@" - ;& - create_specific) - shift - qtap="$1"; shift - parse_create_options "$@" - create_node "$qtap" - $output_qtap && echo "$qtap" - ;; - version) - show_version - ;; - *) - usage_die "Unknown command $1" - ;; -esac -exit 0 - diff --git a/qemu-init-scripts/qemu-conf.example b/qemu-init-scripts/qemu-conf.example new file mode 100644 index 0000000..4911597 --- /dev/null +++ b/qemu-init-scripts/qemu-conf.example @@ -0,0 +1,149 @@ +# Copyright 1999-2013 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# VERSION: 0.3.0 + +# DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD! + +# VMTYPE is auto determined via the file prefix +# ie: kvm.myserver would yield VMTYPE=kvm +# the two types allowed are: qemu, kvm + +# Machine type +# +# The specific qemu binary type +# eg. for VMTYPE=kvm, then /usr/bin/qemu-system-${QEMU_TYPE} +# eg. for VMTYPE=qemu, then /usr/bin/qemu-${QEMU_TYPE} +# QEMU_TYPE="x86_64" +QEMU_TYPE="x86_64" + +## Network settings + +# MAC address +# +# You can generate a new one with this command: +# +# echo "52:54:00:$(dd if=/dev/urandom bs=512 count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\).*$/\1:\2:\3/')" +# +MACADDR=52:54:00:10:34:35 + +# NIC type. +# +# Allowed: +# nat - Automatic qemu NAT network. +# br - Bridged network. Requires 'net.br0' service up and running. +# none - No network. +# +# Defaults to 'nat'. +# +#NIC_TYPE=br + +# NIC model. +# +# See qemu documentation. +# +# Defaults to 'virtio'. +# +#NIC_MODEL= + + +## Storage settings + +# Disk image +DISKIMAGE=/mnt/kvm/gentoo64.qcow2 + +# Drive model. +# +# See qemu documentation. +# +# Defaults to 'virtio'. +# +#DRIVE_MODEL= + +# Drive format. +# +# See qemu documentation. +# +# Defaults to autodetection. +# +#DRIVE_FORMAT= + +# Drive cache type. +# +# See qemu documentation. +# +# Note for zfs filesystems +# ensure you have set zfs +# # zfs set primarycache=all ${tank} +# set DRIVE_CACHE=writeback +# +# Defaults to none. +# +#DRIVE_CACHE= + +## General settings + +# The machine type to use +# see qemu help +# default is Standard PC (i440FX + PIIX, 1996) +# +#MACHINE="q35" + +# Enable/diasble kvm +# +# null value == disabled +# non null value == enabled +# +ENABLE_KVM=1 + +# cpu option +# see qemu help +# +#CPU=host + +# RAM memory allocated for the virtual machine +# +# Defaults to 512M +# +#MEMORY=1G + +# Processor cores allocated for the virtual machine. +# +# Defaults to 1 core +# +#SMP=2 + +# Foreground settings +# +# Allowed: +# vnc=:24 - Runs VNC at :24 +# sdl=:0 - Runs SDL in X.org server running at :0 +# none - Disables foreground +# +# Defaults to 'none'. +# +FOREGROUND="vnc=:24" + +# Low privilege user that runs the virtual machine. +# +# Defaults to 'nobody'. +#DROP_USER= + +# Timeout - How many seconds to wait before trying to hard-kill the virtual +# machine. +# +# Defaults to 300 seconds. +# +#TIMEOUT=60 + +# Any other qemu options you may want to use +# live cd example +OTHER_ARGS="-boot order=cd -cdrom /mnt/archive/gentoo.iso" + +# live-usb image install example, with optional boot menu, adjust the format type as needed +# Note, initial run with an empty primary drive, initial boot fails, +# reboot it (send Cntl-Alt-delete), use the F12 option and select the second drive, after installation +# the primary drive should boot automatically +#OTHER_ARGS="-drive if=virtio,file=/path/to/usb.img,format=raw,cache=writeback,media=disk -boot menu=on" + + diff --git a/qemu-init-scripts/qemu-init-script b/qemu-init-scripts/qemu-init-script new file mode 100755 index 0000000..2d3ce95 --- /dev/null +++ b/qemu-init-scripts/qemu-init-script @@ -0,0 +1,263 @@ +#!/sbin/runscript +# Copyright 2009-2013 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# You are not supposed to run this script directly. Create a symlink +# for the qemu instance you want to run as well as a copy of the +# configuration file and modify it appropriately like so... +# +# ln -s qemu /etc/init.d/kvm.myserver +# cp /etc/conf.d/qemu /etc/conf.d/kvm.myserver +# nano /etc/conf.d/kvm.myserver +# +# where 'kvm' is the virtual machine type ('qemu' and 'kvm' allowed) +# and 'myserver' is the name of your instance. + +VERSION="0.3.0" + +VMNAME=${SVCNAME#*.} +VMTYPE=${SVCNAME%.*} +VM_BINARY= +PIDFILE=/run/vm/${SVCNAME}.pid +MONITOR=/run/vm/${SVCNAME}.monitor +QTAP_FILE=/run/vm/${SVCNAME}.qtap +ENABLE_SDL= + +discern_vm_binary() { + case "$VMTYPE" in + kvm) + VM_BINARY=$(which "qemu-system-${QEMU_TYPE}") + [ -z "$VM_BINARY" ] && eerror "Failed to find the binary for qemu-system-${QEMU_TYPE}" + ;; + qemu) + VM_BINARY=$(which "qemu-${QEMU_TYPE}") + [ -z "$VM_BINARY" ] && eerror "Failed to find the binary for qemu-${QEMU_TYPE}" + ;; + *) + eerror "Failed to discern the binary for $VMTYPE" + ;; + esac + return 0 +} + +discern_foreground() { + if [ -n "$VNC" ]; then + ewarn "VNC option is going away; set FOREGROUND=\"vnc=${VNC}\" instead" + FOREGROUND="vnc=${VNC}" + fi + case "${FOREGROUND:-none}" in + vnc*) + VNC=${FOREGROUND#*=} + if [ -z "$VNC" ]; then + eerror "FOREGROUND vnc is incorrectly set; must specify an address (:1 for example)" + return 1 + fi + ;; + sdl*) + ENABLE_SDL=${FOREGROUND#*=} + if [ -z "${ENABLE_SDL}" ]; then + eerror "FOREGROUND sdl is incorrectly set; must specify a DISPLAY address" + return 1 + fi + ;; + none) + ;; + *) + eerror "Unknown FOREGROUND setting: $FOREGROUND" + return 1 + ;; + esac + return 0 +} + +DROP_USER=${DROP_USER:-nobody} +MEMORY=${MEMORY:-512M} +TIMEOUT=${TIMEOUT:-300} +SMP=${SMP:-1} +export KVM_USER=${KVM_USER:-"root"} + +extra_commands="reboot version" + +depend() { + if [ "$VMNAME" = "$SVCNAME" ]; then + return 0 + fi + + sanity_check || return 1 + + case "$NIC_TYPE" in + br) + need ${NIC_NET_DEPS-net.br0} + ;; + esac +} + +send_command() { + local command="socat -u - UNIX-CONNECT:${MONITOR}" + type -p nc6 > /dev/null && command="nc6 -U ${MONITOR} --send-only" + echo "$@" | ${command} > /dev/null 2>&1 +} + +symlink_check() { + if [ "${VMNAME}" = "${SVCNAME}" ]; then + eerror "You are not supposed to run this script directly. Create a symlink" + eerror "for the qemu instance you want to run as well as a copy of the" + eerror "configuration file and modify it appropriately like so..." + eerror + eerror " ln -s qemu /etc/init.d/kvm.myserver" + eerror " cp /etc/conf.d/qemu /etc/conf.d/kvm.myserver" + eerror " `basename "${EDITOR:-nano}"` /etc/conf.d/kvm.myserver" + eerror + eerror "where 'kvm' is the virtual machine type ('qemu' and 'kvm' allowed)" + eerror "and 'myserver' is the name of your instance." + return 1 + fi +} + +sanity_check() { + symlink_check || return 1 + + DISKIMAGE=$(readlink -f "${DISKIMAGE}") + if [ ! -f "${DISKIMAGE}" -a ! -b "${DISKIMAGE}" ]; then + eerror "couldn't find \$DISKIMAGE '$DISKIMAGE'" + return 1; + fi + discern_vm_binary || return 1 + NIC_TYPE=${NIC_TYPE:-nat} + + discern_foreground || return 1 +} + +start_pre() { + checkpath -d --owner root:root --mode 0644 "${PIDFILE%/*}" \ + "${MONITOR%/*}" +} + +start() { + sanity_check || return 1 + + local NIC_COMMAND=( -net "nic,model=${NIC_MODEL:-virtio},macaddr=${MACADDR}" -net ) + + if [ "${NIC_TYPE}" = "br" ]; then + if [ -n "$QTAP" ]; then + ebegin "using specified QTAP: ${QTAP}" + qtap-manipulate create_specific "${QTAP}" -u "${DROP_USER}" + else + ebegin "creating qtap ${QTAP:-(auto allocating one)}" + QTAP=$(qtap-manipulate create -u "${DROP_USER}") + if [ 0 != $? ]; then + eerror "failed to create qtap interface" + return 1 + fi + fi + echo "${QTAP}" > ${QTAP_FILE} + eend $? + NIC_COMMAND[${#NIC_COMMAND[@]}]="tap,ifname=${QTAP},script=no" + elif [ "${NIC_TYPE}" = "none" ]; then + NIC_COMMAND=( -net none ) + else + NIC_COMMAND[${#NIC_COMMAND[@]}]=user + fi + + local ss_args=( ) + local vm_args=( -daemonize ) + + if [ -n "${ENABLE_SDL}" ]; then + ss_args[${#ss_args[@]}]=--env + ss_args[${#ss_args[@]}]="DISPLAY=${ENABLE_SDL}" + ss_args[${#ss_args[@]}]=--env + local user_home=`getent passwd ${DROP_USER:-root} | cut -d: -f6` + ss_args[${#ss_args[@]}]="XAUTHORITY=$user_home/.Xauthority" + vm_args[${#vm_args[@]}]=-display + vm_args[${#vm_args[@]}]=sdl + elif [ -n "$VNC" ]; then + vm_args[${#vm_args[@]}]=-display + vm_args[${#vm_args[@]}]="vnc=${VNC}" + else + vm_args[${#vm_args[@]}]=-display + vm_args[${#vm_args[@]}]=none + fi + + ebegin "Starting ${VM_BINARY##*/} for ${VMNAME} ${FOREGROUND:+at ${FOREGROUND}}" + set -- start-stop-daemon \ + --start "${VM_BINARY}" \ + --pidfile ${PIDFILE} \ + "${ss_args[@]}" \ + -- \ + "${vm_args[@]}" \ + -pidfile ${PIDFILE} -monitor unix:${MONITOR},server,nowait \ + -runas ${DROP_USER} -name ${VMNAME} \ + -drive file="${DISKIMAGE//,/,,}",if=${DRIVE_MODEL:-virtio},cache=${DRIVE_CACHE:-none}${DRIVE_FORMAT:+,format=${DRIVE_FORMAT}} \ + "${NIC_COMMAND[@]}" \ + ${ENABLE_KVM:+--enable-kvm} ${CPU:+-cpu ${CPU}} \ + ${MACHINE:+-M ${MACHINE}} \ + ${MEMORY:+-m ${MEMORY}} ${SMP:+-smp ${SMP}} \ + ${OTHER_ARGS} + einfo "invoking ${@}" + "${@}" + ret=$? + if [ "0" != "${ret}" -a -n "${QTAP}" ]; then + qtap-manipulate destroy ${QTAP} + fi + + eend ${ret} +} + +reboot() { + symlink_check || return 1 + + ebegin "Rebooting ${VMNAME}" + send_command system_reset + eend $? +} + +stop() { + sanity_check || return 1 + + ebegin "Powering off ${VMNAME}" + send_command system_powerdown + eend $? + + ebegin "waiting up to ${TIMEOUT} seconds for it to die" + local pid + [ -s "${PIDFILE}" ] && pid=$(cat "${PIDFILE}") + if [ -z "$pid" ]; then + eerror "Couldn't find stored pid at '$PIDFILE'; user will have to manually kill kvm" + eerror "Will attempt to destroy qtap despite." + eend 1 + else + local ret=1 + for x in $(seq 0 ${TIMEOUT}); do + if kill -0 "${pid}" > /dev/null 2>&1; then + sleep 1s + continue + fi + ret=0 + break + done + eend $ret + fi + + ebegin "Stopping ${VM_BINARY##*/} for ${VMNAME}" + if kill -0 "${pid}" > /dev/null 2>&1; then + start-stop-daemon --stop "${VM_BINARY}" \ + --user "${DROP_USER}" \ + --pidfile "${PIDFILE}" \ + --quiet + eend $? + else + eend 0 # no need to kill process if it is dead :P + fi + local qtap + [ -s "${QTAP_FILE}" ] && qtap=$(cat "${QTAP_FILE}") + if [ -n "$qtap" ]; then + ebegin "destroying qtap ${qtap}" + qtap-manipulate destroy ${qtap} + eend $? + fi +} + +version() { + echo "qemu-init-scripts version: ${VERSION}" +} + diff --git a/qemu-init-scripts/qtap-manipulate b/qemu-init-scripts/qtap-manipulate new file mode 100755 index 0000000..516e1ca --- /dev/null +++ b/qemu-init-scripts/qtap-manipulate @@ -0,0 +1,128 @@ +#!/bin/sh +# Copyright 1999-2012 Gentoo Foundation +# Distributed under the terms of the GNU General Public Licens + +VERSION="0.3.0" + +has() { + local desired=$1 x + shift + for x in "$@"; do + [ "$desired" = "$x" ] && return 0; + done + return 1 +} + +find_available_node() { + local val=$(ip addr | grep -i '.*: qtap' | cut -d: -f2 | cut -d@ -f1) + local pos=0 + while has qtap${pos} ${val##*()}; do + pos=$(( $pos + 1 )) + done + echo qtap${pos} +} + +create_node() { + local qtap="$1" + shift + tunctl -b -t "${qtap}" "$@" > /dev/null || die "tunctl failed" + brctl addif br0 "${qtap}" || die "brctl failed" + ip link set "${qtap}" up || die "ip link set ${qtap} failed" + ip link set "${qtap}" promisc on || die "ip link set ${qtap} promiscuos mode failed" +} + +destroy_node() { + issue= + ip link set ${1} down || { echo "ip link set ${1} down failed";issue=1; } + brctl delif br0 ${1} || { echo "brctl failed";issue=2; } + tunctl -d ${1} > /dev/null || { echo "tunctl failed";issue=3;} + [ -n "${issue}" ] && exit $(( $issue )) +} + +die() { + echo "$@" >&2 + exit 1 +} + +usage() { + echo "commands available:" + echo "create-specific qtap-name [ -u user ] [ -g group ]" + echo "create [ -u user ] [ -g group ]" + echo "destroy qtap-name" + echo "version" + echo +} + +usage_die() { + usage + die "$@" +} + +show_version() { + echo "qtap-manipulate version: ${VERSION}" +} + +create_user= +create_group= + +parse_create_options() { + while [ $# -ne 0 ]; do + local x="$1" + case "$x" in + -u=*) + shift + set -- "-u" "${x#-u=}" "$@" + ;& + -u) + shift + [ -z "$1" ] && die "-u requires an argument" + create_user="$1" + shift + ;; + -g=*) + shift + set -- "-g" "${x#-u=}" "$@" + ;& + -g) + shift + [ -z "$1" ] && die "-g requires an argument" + create_group="$2" + shift + ;; + *) + die "unknown option $1" + esac + done +} + +output_qtap=false +case "$1" in + destroy) + shift + [ $# -eq 0 ] && usage_die "destroy requires a second argument" + [ $# -gt 1 ] && usage_die "no idea what to do with args: $@" + destroy_node "$1" + ;; + create) + output_qtap=true + qtap=$(find_available_node) + [ -z "$qtap" ] && die "failed to find a qtap node to use" + shift + set -- create_specific "${qtap}" "$@" + ;& + create_specific) + shift + qtap="$1"; shift + parse_create_options "$@" + create_node "$qtap" + $output_qtap && echo "$qtap" + ;; + version) + show_version + ;; + *) + usage_die "Unknown command $1" + ;; +esac +exit 0 + -- cgit v1.2.3-18-g5258