#!/bin/sh # kvm-shell - a small interface to kvm for regular users # v1.0-0 # Copyright (C) 2009 Michael Kress # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # # INSTALL: # 1) create these entries for your users in /etc/sudoers (use visudo) : # User_Alias KVMUSERS = vuser1,vuser2 # Cmnd_Alias KVM = /usr/bin/virsh # KVMUSERS ALL = NOPASSWD: KVM # 2) Create /usr/local/etc/kvm-shell-users (see config variable below) : # Format: # # Example: # vuser1 vm1 # vuser2 vm1 vm2 vm3 # Description: List here which users are allowed to control which vms # # KNOWN BUGS: # * To update the status, select another menu item and return to the current one # # CHANGELOG: # v1.0-0 / 2009-12-23 / Initial release # configuration USERSFILE=/usr/local/etc/kvm-shell-users CURRENTUSER=$USER ### test: CURRENTUSER=vuser1 SUDO=/usr/bin/sudo VIRSH=/usr/bin/virsh VNCBASEPORT=5900 # set DEBUG to 1 to see some messages, otherwise leave on 0 DEBUG=0 # change nothing below here MYEXIT=0 function select_vm { # PARAMETERS: none # DESCRIPTION: extract the vms which the current user is allowed to control and display them in a little menu AVAILABLEVMS=`grep $CURRENTUSER $USERSFILE | sed -e "s/$CURRENTUSER\(.*\)/\1/g"` MENUITEMS="" COUNTER=0 NEWAVAILABLEVMS="" for i in $AVAILABLEVMS do $SUDO $VIRSH domstate $i 2>/dev/null if [ "$?" = "0" ]; then NEWAVAILABLEVMS="${NEWAVAILABLEVMS} ${i}" COUNTER=$((COUNTER+1)) STATE=`$SUDO $VIRSH domstate $i|grep -v "^$" | sed -e "s/ /_/g"` MENUITEMS="$MENUITEMS $i $STATE" fi done AVAILABLEVMS=$NEWAVAILABLEVMS [ $DEBUG -eq 1 ] && echo $MENUITEMS selectedvm=`dialog --stdout --title "User $CURRENTUSER - please select your VM to manage" --menu "Available VMs:" 15 55 $COUNTER $MENUITEMS` ret=$? case $ret in 0) [ $DEBUG -eq 1 ] && echo "selected: $selectedvm" while [ $MYEXIT -ne 2 ]; do select_action done MYEXIT=0 ;; 1) [ $DEBUG -eq 1 ] && echo "You pressed cancel." MYEXIT=1;; 255) [ $DEBUG -eq 1 ] && echo "You hit Esc." MYEXIT=1;; esac } function select_action { # PARAMETERS: none # DESCRIPTION: display actions and execute them on the selected vm # REQUIRES: $selectedvm filled with a valid vm # actions displayed in menu: # - dominfo # - vncdisplay # - start # - shutdown # - reboot # - destroy ### TODO: Check if $selectedvm is really a valid vm (should be ok, but still ...) STATE=`$SUDO $VIRSH domstate $selectedvm|grep -v "^$" | sed -e "s/ /_/g"` selectedaction=`dialog --stdout --title "User $CURRENTUSER - please select action for VM $selectedvm" --menu "Available actions for VM $selectedvm ($STATE):" 15 55 5 "dominfo" "" "vncdisplay" "" "start" "" "shutdown" "" "reboot" "" "destroy" ""` ret=$? case $ret in 0) echo "$SUDO $VIRSH $selectedaction $selectedvm" case $selectedaction in dominfo) $SUDO $VIRSH $selectedaction $selectedvm ;; vncdisplay) VNCDSP=`$SUDO $VIRSH vncdisplay $selectedvm | grep -v "^$" | sed -e "s/://g"` VNCDSP=$((VNCBASEPORT+VNCDSP)) echo "VNC port: $VNCDSP" ;; start) $SUDO $VIRSH $selectedaction $selectedvm ;; shutdown) $SUDO $VIRSH $selectedaction $selectedvm ;; reboot) $SUDO $VIRSH $selectedaction $selectedvm ;; destroy) $SUDO $VIRSH $selectedaction $selectedvm ;; esac sleep 1 [ "$selectedaction" != "dominfo" ] && $SUDO $VIRSH dominfo $selectedvm read -p "[Hit Return]" x ;; 1) [ $DEBUG -eq 1 ] && echo "You pressed cancel." MYEXIT=2 ;; 255) [ $DEBUG -eq 1 ] && echo "You hit Esc." MYEXIT=2 ;; esac } # main loop while [ $MYEXIT -ne 1 ]; do select_vm done exit 0