Guest User

Untitled

a guest
Feb 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This sctipt can be added in /etc/inittab in any system with traditional init systems
  4. # to replace the login prompt whith a selection of VMs
  5.  
  6. # This script uses virsh to get list of all local lxc and qemu VMs managed using libvirt.
  7. # The list will be displayed using dialog. When selecting a VM, this script attempts to
  8. # get a spice or vnc display. It will connect to those with spice-client or directvnc.
  9. # If no display is available, virsh console is used to connect to the vm console.
  10.  
  11. login_target_list="";
  12. login_target_list_count=0
  13.  
  14. function quote {
  15. local quoted=${1//\'/\'\\\'\'};
  16. printf " '%s'" "$quoted"
  17. }
  18.  
  19. function getTitle {
  20. case "$1" in
  21. lxc) virsh -c lxc:/// desc --title "$2";;
  22. qemu) virsh -c qemu:///system desc --title "$2";;
  23. esac
  24. }
  25.  
  26. function addLoginTarget {
  27. i=$login_target_list_count
  28. login_target_list="$login_target_list""$(
  29. printf "%s" "$2" | grep -v ^$ |
  30. while IFS= read -r name || [[ -n "$name" ]]
  31. do
  32. i="$(expr $i + 1)"
  33. echo -n '"'
  34. echo -n "$i"
  35. quote "$1"
  36. quote "$name"
  37. echo '"'
  38. done
  39. )
  40. "
  41. login_target_list_count="$(printf "%s" "$login_target_list"|wc -l)"
  42. }
  43.  
  44. function chooseTarget {
  45. entries=$(for entry in "${login_list[@]}"; do {
  46. eval "e=($entry)"
  47. title="$(getTitle "${e[1]}" "${e[2]}")"
  48. echo -n "${e[0]}"
  49. quote "$(printf "%-6s %-10s %s" "${e[1]}" "${e[2]}" "$title")"
  50. echo -n " "
  51. } done)
  52. target_index=$(eval "\
  53. dialog --stdout --cancel-label "Aktualisieren"\
  54. --menu \"Umgebung waehlen (\$(basename \"\$(tty)\"))\" 0 0 \
  55. $login_target_list_count $entries
  56. ")
  57. if [ -z "$target_index" ]
  58. then target=(-1 exit none)
  59. else eval "target=(${login_list[$(expr $target_index - 1)]})"
  60. fi
  61. clear
  62. }
  63.  
  64. function buildTargetList {
  65.  
  66. addLoginTarget login "$(hostname)"
  67. addLoginTarget lxc "$(virsh -c lxc:/// list --all --name)"
  68. addLoginTarget qemu "$(virsh -c qemu:///system list --all --name)"
  69.  
  70. eval "login_list=($login_target_list)"
  71. }
  72.  
  73. function use_exit {
  74. true
  75. }
  76.  
  77. function use_login {
  78. login
  79. }
  80.  
  81. function virsh_show_display {
  82. uri="$(virsh domdisplay --type spice "$domain" 2>/dev/null)"
  83. if [ $? == 0 ]
  84. then
  85. printf "%s" "$uri" | grep -o '[^/]*$' | tr ':' '\n' | {
  86. read domain
  87. read port
  88. export domain
  89. export port
  90. CMD_LINE='spicec -f -h "$domain" -p "$port"' xinit /usr/local/bin/meval
  91. unset domain
  92. unset port
  93. }
  94. return 0
  95. fi
  96. uri="$(virsh domdisplay --type vnc "$domain" 2>/dev/null)"
  97. if [ $? == 0 ]
  98. then
  99. directvnc "$(printf "%s" "$uri"|grep -o '[^/]*$')" -m '' -e rre -b 24
  100. return 0
  101. fi
  102. virsh console "$domain"
  103. return $?
  104. }
  105.  
  106. function use_libvirt {
  107. domain=$1
  108. clear
  109. virsh start "$domain" 2>/dev/null
  110. virsh_show_display
  111. }
  112.  
  113. function use_lxc {
  114. LIBVIRT_DEFAULT_URI=lxc:/// use_libvirt "$@"
  115. }
  116.  
  117. function use_qemu {
  118. LIBVIRT_DEFAULT_URI=qemu:///system use_libvirt "$@"
  119. }
  120.  
  121. buildTargetList
  122. chooseTarget
  123.  
  124. type=${target[1]}
  125. name=${target[2]}
  126.  
  127. "use_$type" "$name"
Add Comment
Please, Sign In to add comment