diff --git a/roles/lmn_vm/files/vm-create b/roles/lmn_vm/files/vm-create index b669890..4fb2a67 100755 --- a/roles/lmn_vm/files/vm-create +++ b/roles/lmn_vm/files/vm-create @@ -38,29 +38,10 @@ fi # change to image-directory cd "${VM_DIR}" -if { [[ ! -f "${VM_NAME}.xml" ]] && [[ ! -f "${VM_SYSDIR}/${VM_NAME}.xml" ]]; } || [[ ! -f "${VM_NAME}.qcow2" ]]; then - echo "xml or qcow2 File does not exists." >&2 +if [[ ! -f "${VM_NAME}.qcow2" ]]; then + echo "qcow2 File does not exists." >&2 exit 1 fi qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-${VM_CLONE}.qcow2" chmod a-w "${VM_NAME}-${VM_CLONE}.qcow2" - -# virsh --connect=qemu:///system dumpxml "${VM_NAME}" > "xml/${VM_NAME}-${VM_CLONE}.xml" -# copy machine-definition-file -if [[ -f "${VM_NAME}.xml" ]]; then - cp "${VM_NAME}.xml" "${VM_NAME}-${VM_CLONE}.xml" -elif [[ -f "${VM_SYSDIR}/${VM_NAME}.xml" ]]; then - cp "${VM_SYSDIR}/${VM_NAME}.xml" "${VM_NAME}-${VM_CLONE}.xml" -else - echo "no machine definition file found" >&2 - exit 1 -fi - -# hardware addresses need to be removed, libvirt will assign -# new addresses automatically -# and actually rename the vm: (this also updates the storage path) -sed -i -E \ - -e '/.+<\/uuid>/d' \ - -e '/.+mac address.+[[:xdigit:]:]{17}.+/d' \ - -e "s/${VM_NAME}/${VM_NAME}-${VM_CLONE}/" "${VM_NAME}-${VM_CLONE}.xml" diff --git a/roles/lmn_vm/files/vm-link-images b/roles/lmn_vm/files/vm-link-images index a3a06df..efa083c 100755 --- a/roles/lmn_vm/files/vm-link-images +++ b/roles/lmn_vm/files/vm-link-images @@ -22,8 +22,3 @@ shift "$((OPTIND -1))" for i in *.qcow2; do [[ -f "${VM_DIR}/${i}" ]] || ln "${i}" "${VM_DIR}/${i}" done - -# link system-VM-Machine-Definitions to User VM Directory -for i in *.xml; do - [[ -f "${VM_DIR}/${i}" ]] || ln "${i}" "${VM_DIR}/${i}" -done diff --git a/roles/lmn_vm/files/vm-rebase b/roles/lmn_vm/files/vm-rebase index cc6292e..b235760 100755 --- a/roles/lmn_vm/files/vm-rebase +++ b/roles/lmn_vm/files/vm-rebase @@ -70,17 +70,6 @@ fi # rebasing disk image qemu-img rebase -f qcow2 -b "${NEWBASE}" -F qcow2 "${VM_NAME}.qcow2" if [[ -v NEWNAME ]]; then - # copy and adapt machine definition file - CURRENTNAME="${CURRENTBASE/.qcow2/}" - if [[ -f "${CURRENTNAME}.xml" ]]; then - cp "${CURRENTNAME}.xml" "${NEWNAME}.xml" - elif [[ -f "${VM_SYSDIR}/${CURRENTNAME}.xml" ]]; then - cp "${VM_SYSDIR}/${CURRENTNAME}.xml" "${NEWNAME}.xml" - else - echo "no machine definition file found" >&2 - exit 1 - fi - sed -i "s/${CURRENTNAME}/${NEWNAME}/" "${NEWNAME}.xml" NEWNAME="${NEWNAME}.qcow2" else rm "${CURRENTBASE}" diff --git a/roles/lmn_vm/files/vm-run b/roles/lmn_vm/files/vm-run index 0e11ad0..eef1010 100755 --- a/roles/lmn_vm/files/vm-run +++ b/roles/lmn_vm/files/vm-run @@ -5,12 +5,18 @@ set -eu show_help() { cat << EOF >&2 -Usage: $(basename "$0") [-n] vmname" +Usage: $(basename "$0") [options] vmname" Create a new clone, start the vm (if not yet running) and run virt-viewer. Squid-Proxy will be started too. - -n new clone will be created, even if exists - -p new clone will be created persistent, so available after reboot too - -s qemu:///system instead of default qemu:///session +options: + -n|--new new clone will be created, even if exists + -p|--persistent new clone will be created persistent, so available after reboot too + -s|--system qemu:///system instead of default qemu:///session + --memory sizeMB memory size in MB + --cpu num number of CPUs + --os OS operating system (win10|linux|..) + --data-disk size additional data-disk + --options options additional options for virt-install command EOF } @@ -23,7 +29,7 @@ exit_script() { check_images() { # sync vm-torrents and machine definition file - sudo -u lmnsynci /usr/local/bin/vm-sync get_file "${VM_NAME}.xml" "${VM_NAME}.qcow2.torrent" + sudo -u lmnsynci /usr/local/bin/vm-sync get_file "${VM_NAME}.qcow2.torrent" [[ -f "${VM_NAME}" ]] && sudo -u lmnsynci /usr/local/bin/vm-sync delete_outdated_image "${VM_NAME}.qcow2" BACKINGARRAY=() @@ -74,10 +80,9 @@ check_images() { create_clone() { local VM_NAME="$1" - local VM_XML="${VM_DIR}/${VM_NAME}-clone.xml" - if ! [[ -f "${VM_SYSDIR}/${VM_NAME}.xml" && -f "${VM_SYSDIR}/${VM_NAME}.qcow2" ]] && ! [[ -f "${VM_DIR}/${VM_NAME}.xml" && -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then - echo "xml or qcow2 File does not exists." >&2 + if ! [[ -f "${VM_SYSDIR}/${VM_NAME}.qcow2" || -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then + echo "qcow2 File does not exists." >&2 exit 1 fi @@ -93,49 +98,65 @@ create_clone() { cd "${VM_DIR}" qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-clone.qcow2" - # Create machine-definition-file - cp "${VM_DIR}/${VM_NAME}.xml" "${VM_XML}" - - # set VM_DIR: - sed -i "s:VMIMAGEDIR:${VM_DIR}:" "${VM_XML}" - # and actually rename the vm (this also updates part of the storage path): - sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}" - # set virtiofs-Socket - sed -i "s:VIRTIOFSSOCKET:/run/user/${UID}/virtiofs-${VM_NAME}.sock:" "${VM_XML}" - - # find macvtap interface MAC address: - MAC="$(ip link | grep -A1 "vm-macvtap" | - sed -nE "s%\s+link/ether ([[:xdigit:]:]{17}) .+%\1%p")" - sed -i -E -e "s/MACMACVTAP/$MAC/" "${VM_XML}" } QEMU='qemu:///session' NEWCLONE=0 PERSISTENT=0 +LIBVIRTOSINFO="win10" +LIBVIRTOPTS="" source /etc/lmn/vm.conf -while getopts ':pns' OPTION; do - case "$OPTION" in - p) - PERSISTENT=1 - VM_DIR="${VM_DIR_PERSISTENT}" - ;; - n) - NEWCLONE=1 - ;; - s) - QEMU='qemu:///system' - ;; - ?) - show_help - exit 1 - ;; - esac -done +TEMP=$(getopt -o no:ps --long new,options:,persistent:,system,memory:,data-disk:,cpu:,os:,help -n $0 -- "$@") +if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi -shift "$((OPTIND -1))" +eval set -- "$TEMP" + +while true; do + case "$1" in + -p | --persistent ) + PERSISTENT=1; + VM_DIR="${VM_DIR_PERSISTENT}" + shift + ;; + -n | --new ) + NEWCLONE=1 + shift + ;; + -s | --system ) + QEMU='qemu:///system' + shift + ;; + -o | --options ) + LIBVIRTOPTS=$2 + shift 2 + ;; + --data-disk ) + LIBVIRTOPTS="${LIBVIRTOPTS} --disk ${VM_DIR}/data.qcow2,size=$2,sparse=yes" + shift 2 + ;; + --memory ) + LIBVIRTOPTS="${LIBVIRTOPTS} --memory $2" + shift 2 + ;; + --cpu ) + LIBVIRTOPTS="${LIBVIRTOPTS} --vcpu $2" + shift 2 + ;; + --os ) + LIBVIRTOSINFO=$2 + shift 2 + ;; + --help ) + show_help + exit 1 + ;; + -- ) shift; break ;; + * ) break ;; + esac +done # if less than one arguments supplied, display usage if [[ $# -ne 1 ]] ; then @@ -163,11 +184,45 @@ if ! virsh --connect="${QEMU}" list | grep "${VM_NAME}-clone"; then fi # delete the old vm virsh --connect=qemu:///session undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist" - # finally, create the new vm - virsh --connect=qemu:///session define "${VM_DIR}/${VM_NAME}-clone.xml" #trap exit_script SIGHUP SIGINT SIGTERM + + # start virtiofsd-service [[ "${QEMU}" = 'qemu:///session' ]] && sudo /usr/local/bin/vm-virtiofsd "${VM_NAME}" - virsh --connect="${QEMU}" start "${VM_NAME}-clone" + + # finally, create the new vm + + if ip link | grep virbr1; then + LIBVIRTOPTS="${LIBVIRTOPTS} --network=bridge=virbr1,model.type=virtio" + fi + + +# TODO +# # find macvtap interface MAC address: +# MAC="$(ip link | grep -A1 "vm-macvtap" | +# sed -nE "s%\s+link/ether ([[:xdigit:]:]{17}) .+%\1%p")" +# sed -i -E -e "s/MACMACVTAP/$MAC/" "${VM_XML}" + + virt-install \ + --osinfo "${LIBVIRTOSINFO}" \ + --name "${VM_NAME}-clone" \ + --import \ + --clock hpet_present=yes \ + --features hyperv.synic.state=on,xpath1.set=./hyperv/vpindex/@state=on \ + --memorybacking source.type=memfd,access.mode=shared \ + --disk "${VM_DIR}/${VM_NAME}-clone.qcow2" \ + --network=bridge=virbr0,model.type=virtio \ + --filesystem driver.type=virtiofs,accessmode=passthrough,target.dir=virtiofs,xpath1.set=./source/@socket="/run/user/${UID}/virtiofs-${VM_NAME}.sock" \ + --check path_in_use=off \ + --connect="${QEMU}" \ + --noautoconsole \ + ${LIBVIRTOPTS} +# --dry-run \ +# --print-xml \ +# > /tmp/vm.xml +# --features hyperv.synic.state=on,xpath1.set=./hyperv/vpindex/@state=on,xpath2.set=./hyperv/stimer/@state=on \ +# --network type=ethernet,target.dev=vm-macvtap,xpath1.set=./target/@managed=no \ + +# virsh --connect="${QEMU}" start "${VM_NAME}-clone" fi echo "starting viewer" trap exit_script SIGHUP SIGINT SIGTERM diff --git a/roles/lmn_vm/files/vm-upload b/roles/lmn_vm/files/vm-upload index ddb83e0..732b59b 100755 --- a/roles/lmn_vm/files/vm-upload +++ b/roles/lmn_vm/files/vm-upload @@ -5,7 +5,7 @@ set -eu show_help() { cat << EOF >&2 Usage: $(basename "$0") vmname" -Create torrent and upload disk and xml-VM-Definiton on server. +Create torrent and upload disk on server. EOF } @@ -16,11 +16,6 @@ upload_image() { echo "File not found ${VM_NAME}.qcow2" >&2 exit 1 fi - # check if VM-Machine-Definition XML exists - if [[ ! (-f "${VM_SYSDIR}/${VM_NAME}.xml" || -f "${VM_DIR}/${VM_NAME}.xml") ]]; then - echo "File not found ${VM_NAME}.xml" >&2 - exit 1 - fi sudo vm-aria2 stop "${VM_NAME}" || echo "VMImage-torrent not running" # link private VM-Diskimage to system-Dir if [[ -f "${VM_DIR}/${VM_NAME}.qcow2" \ @@ -30,19 +25,9 @@ upload_image() { chown lmnsynci:lmnsynci "${VM_DIR}/${VM_NAME}.qcow2" ln -f "${VM_DIR}/${VM_NAME}.qcow2" "${VM_SYSDIR}/${VM_NAME}.qcow2" fi - # copy private VM-Maschine-Definition XML to system-Dir - if [[ -f "${VM_DIR}/${VM_NAME}.xml" \ - && ( -f "${VM_SYSDIR}/${VM_NAME}.xml" && $(cmp -s "${VM_DIR}/${VM_NAME}.xml" "${VM_SYSDIR}/${VM_NAME}.xml") \ - || ! -f "${VM_SYSDIR}/${VM_NAME}.xml") ]]; then - echo "copy private VM-Maschine-Definition XML to system-dir" - chown lmnsynci:lmnsynci "${VM_DIR}/${VM_NAME}.xml" - cp -a "${VM_DIR}/${VM_NAME}.xml" "${VM_SYSDIR}" - fi cd "${VM_SYSDIR}" uploadseed --server "${SEEDBOX_HOST}:${SEEDBOX_RPC_PORT}" --dht-port "${SEEDBOX_PORT}" \ --pwdfile "${SEEDBOX_PWFILE}" --no-cert "${VM_NAME}.qcow2" - uploadseed --server "${SEEDBOX_HOST}:${SEEDBOX_RPC_PORT}" --dht-port "${SEEDBOX_PORT}" \ - --pwdfile "${SEEDBOX_PWFILE}" --no-cert "${VM_NAME}.xml" } source /etc/lmn/vm.conf