Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This sctipt can be added in /etc/inittab in any system with traditional init systems
- # to replace the login prompt whith a selection of VMs
- # This script uses virsh to get list of all local lxc and qemu VMs managed using libvirt.
- # The list will be displayed using dialog. When selecting a VM, this script attempts to
- # get a spice or vnc display. It will connect to those with spice-client or directvnc.
- # If no display is available, virsh console is used to connect to the vm console.
- login_target_list="";
- login_target_list_count=0
- function quote {
- local quoted=${1//\'/\'\\\'\'};
- printf " '%s'" "$quoted"
- }
- function getTitle {
- case "$1" in
- lxc) virsh -c lxc:/// desc --title "$2";;
- qemu) virsh -c qemu:///system desc --title "$2";;
- esac
- }
- function addLoginTarget {
- i=$login_target_list_count
- login_target_list="$login_target_list""$(
- printf "%s" "$2" | grep -v ^$ |
- while IFS= read -r name || [[ -n "$name" ]]
- do
- i="$(expr $i + 1)"
- echo -n '"'
- echo -n "$i"
- quote "$1"
- quote "$name"
- echo '"'
- done
- )
- "
- login_target_list_count="$(printf "%s" "$login_target_list"|wc -l)"
- }
- function chooseTarget {
- entries=$(for entry in "${login_list[@]}"; do {
- eval "e=($entry)"
- title="$(getTitle "${e[1]}" "${e[2]}")"
- echo -n "${e[0]}"
- quote "$(printf "%-6s %-10s %s" "${e[1]}" "${e[2]}" "$title")"
- echo -n " "
- } done)
- target_index=$(eval "\
- dialog --stdout --cancel-label "Aktualisieren"\
- --menu \"Umgebung waehlen (\$(basename \"\$(tty)\"))\" 0 0 \
- $login_target_list_count $entries
- ")
- if [ -z "$target_index" ]
- then target=(-1 exit none)
- else eval "target=(${login_list[$(expr $target_index - 1)]})"
- fi
- clear
- }
- function buildTargetList {
- addLoginTarget login "$(hostname)"
- addLoginTarget lxc "$(virsh -c lxc:/// list --all --name)"
- addLoginTarget qemu "$(virsh -c qemu:///system list --all --name)"
- eval "login_list=($login_target_list)"
- }
- function use_exit {
- true
- }
- function use_login {
- login
- }
- function virsh_show_display {
- uri="$(virsh domdisplay --type spice "$domain" 2>/dev/null)"
- if [ $? == 0 ]
- then
- printf "%s" "$uri" | grep -o '[^/]*$' | tr ':' '\n' | {
- read domain
- read port
- export domain
- export port
- CMD_LINE='spicec -f -h "$domain" -p "$port"' xinit /usr/local/bin/meval
- unset domain
- unset port
- }
- return 0
- fi
- uri="$(virsh domdisplay --type vnc "$domain" 2>/dev/null)"
- if [ $? == 0 ]
- then
- directvnc "$(printf "%s" "$uri"|grep -o '[^/]*$')" -m '' -e rre -b 24
- return 0
- fi
- virsh console "$domain"
- return $?
- }
- function use_libvirt {
- domain=$1
- clear
- virsh start "$domain" 2>/dev/null
- virsh_show_display
- }
- function use_lxc {
- LIBVIRT_DEFAULT_URI=lxc:/// use_libvirt "$@"
- }
- function use_qemu {
- LIBVIRT_DEFAULT_URI=qemu:///system use_libvirt "$@"
- }
- buildTargetList
- chooseTarget
- type=${target[1]}
- name=${target[2]}
- "use_$type" "$name"
Add Comment
Please, Sign In to add comment