diff --git a/kvm-host/files/add-vm.sh b/kvm-host/files/add-vm.sh index 3186db43..49ca6f46 100755 --- a/kvm-host/files/add-vm.sh +++ b/kvm-host/files/add-vm.sh @@ -37,6 +37,8 @@ disks="${disks:-}" bridgeName="${bridgeName:-br0}" doDryRun=${doDryRun:-false} isoImagePath="${isoImagePath:-}" +debianVersion="${debianAuto:-stable}" +preseedURL="${preseedURL:-}" export DIALOGOUT=$(mktemp --tmpdir=/tmp addvm.XXX) # TODO: How to replace _ with a space?? @@ -184,7 +186,7 @@ ${drbdadm} adjust "$vmName" ssh $slaveKVMIP ${drbdadm} adjust "$vmName" ${drbdadm} -- --overwrite-data-of-peer primary "$vmName" -if !($doDryRun); then +if ! ($doDryRun); then sleep 5 && drbd-overview | tail -4 drbdDiskPath="/dev/drbd/by-res/${vmName}/0" @@ -200,6 +202,7 @@ fi virtHome="" [ "$volhomeDisk" != "none" ] && virtHome="--disk path=/dev/drbd/by-disk/${volhomeDisk}/${vmName}_home,bus=virtio,io=threads,cache=none,format=raw" bootMode="--pxe" +[ -n "${preseedURL}" ] && bootMode="--location https://deb.debian.org/debian/dists/${debianVersion}/main/installer-amd64/ --extra-args \"auto=true priority=critical url=${preseedURL} hostname=${vmName}\"" [ -f "$isoImagePath" ] && bootMode="--cdrom=$isoImagePath" dryRun virt-install --connect=qemu:///system \ diff --git a/kvm-host/files/kvmstats.cron.j2 b/kvm-host/files/kvmstats.cron.j2 new file mode 100644 index 00000000..d34d3d68 --- /dev/null +++ b/kvm-host/files/kvmstats.cron.j2 @@ -0,0 +1,4 @@ +#!/bin/sh + +{{ kvm_scripts_dir }}/kvmstats -a -o html > /var/www/kvmstats.html +/bin/chmod go+r /var/www/kvmstats.html \ No newline at end of file diff --git a/kvm-host/files/kvmstats.sh b/kvm-host/files/kvmstats.sh index 588ef863..cf2416b9 100755 --- a/kvm-host/files/kvmstats.sh +++ b/kvm-host/files/kvmstats.sh @@ -1,46 +1,54 @@ #!/bin/sh -# NOTE: kvmstats relies on the hxselect(1) command to parse virsh' xml -# files. On Debian, this command is provided by the 'html-xml-utils' -# package. - -set -e -u - -usage () { - echo 'usage: kvmstats.sh [-a] [-u K|M|G]' +error () { + echo "$0": "$@" >&2 exit 1 } -for DEP in hxselect lvs tempfile bc +usage () { + echo 'usage:' "$0" '[-a] [-u k|m|g] [-o human|html|csv]' >&2 + exit 1 +} + +for DEP in bc virsh do - if [ -z "$(which $DEP)" ] - then - echo "kvmstats.sh: $DEP not found in \$PATH" 1>&2 - exit 1 - fi + command -v "$DEP" > /dev/null || error "$DEP" 'command not found' done -POW=$(echo 1024 ^ 3 | bc) -while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]' +POW="$(echo '1024 ^ 3' | bc)" +FMT='human' +while [ "$#" -ne 0 ] do - case $1 in + case "$1" in + '-a') + SHOW_AVAIL='y' + ;; + '-o') + case "$2" in + 'csv'|'html'|'human') + FMT="$2" + ;; + *) + usage + ;; + esac + shift + ;; '-u') - case $2 in - 'K') - POW=$(echo 1024 ^ 1 | bc) + case "$2" in + 'k') + POW="$(echo '1024 ^ 1' | bc)" ;; - 'M') - POW=$(echo 1024 ^ 2 | bc) + 'm') + POW="$(echo '1024 ^ 2' | bc)" ;; - 'G') - POW=$(echo 1024 ^ 3 | bc) + 'g') + POW="$(echo '1024 ^ 3' | bc)" ;; *) usage esac - ;; - '-a') - SHOW_AVAIL=y + shift ;; *) usage @@ -48,47 +56,41 @@ do shift done -# since libvirt seems to store memoy in KiB, POW must be lowered by 1 -POW=$((POW / 1024)) - -TMPFILE=$(tempfile -s kvmstats) -LVSOUT=$(tempfile -s kvmstats) - -lvs --units b --nosuffix >"$LVSOUT" - -for VM in $(virsh list --all --name) +for VM in $(virsh list --name --all) do - VCPU=$(hxselect -c 'domain vcpu' /dev/null | awk 'BEGIN{ret=1}$1~/^actual$/{print $2 / '$((POW / 1024))';ret=0}END{exit ret}' || + virsh dumpxml "$VM" | awk -F'[<>]' '$2~/^memory unit/{print $3/'$((POW / 1024))'}' + + # disk + for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | awk '{print $1}') do - case $DEV in - /dev/drbd/*) - DISK=$(awk "/$VM/ { ans += \$NF } END { print ans / 1024 ^ 3 }" <"$LVSOUT") - break # avoid to compute DISK for each disk - ;; - *.qcow2) - DISK=$(du -sBG "$DEV" | awk '{ print substr($1, 0, length($1) - 1) }') - ;; - *) - DISK=0 - esac - done - RUNNING=$(virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no) - echo "$VM" "$VCPU" "$RAM" "$DISK" "$RUNNING" -done >"$TMPFILE" + virsh domblkinfo "$VM" "$BLK" 2> /dev/null + done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }' -( + # state + virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no +done | xargs -n5 | { echo vm vcpu ram disk running - cat "$TMPFILE" - awk '/yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }' <"$TMPFILE" - if [ $SHOW_AVAIL ] - then - AV_CPU=$(awk '/^processor/ { cpu++ } END { print cpu }' /proc/cpuinfo) - AV_MEM=$(awk '/^MemTotal:/ { print int($2 / 1024 ^ 2) }' /proc/meminfo) - echo AVAILABLE "$AV_CPU" "$AV_MEM" - fi -) | column -t - -rm "$TMPFILE" "$LVSOUT" + awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }' + test "$SHOW_AVAIL" && { + nproc + awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo + } | xargs -r printf 'AVAILABLE %s %s %s %s\n' +} | case "$FMT" in +'human') + column -t + ;; +'html') + awk 'BEGIN{print "\n"}{printf "";for(i=1;i<=NF;i++)printf "", $i;print ""}END{print "
%s
\n"}' + ;; +'csv') + tr ' ' ',' + ;; +esac diff --git a/kvm-host/tasks/tools.yml b/kvm-host/tasks/tools.yml index 56caa6ea..83845a31 100644 --- a/kvm-host/tasks/tools.yml +++ b/kvm-host/tasks/tools.yml @@ -37,6 +37,21 @@ group: root force: yes +- name: kvmstats cron is present + template: + src: kvmstats.cron.j2 + dest: "/etc/cron.hourly/kvmstats" + mode: "0755" + owner: root + group: root + +- name: entry for kvmstats in web page is present + lineinfile: + dest: /var/www/index.html + insertbefore: '' + line: '
  • kvmstats
  • ' + + # backward compatibility - name: remove old migrate-vm script