Advertisement
pepoluan

install-xe-guest-utilities.sh for Gentoo

Jul 6th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.20 KB | None | 0 0
  1. #!/bin/bash
  2. # Copyright (c) 2011 Pandu E Poluan <pandu@poluan.info>
  3. # Distributed under the terms of the GNU General Public License v2 or newer
  4.  
  5. # Description: Script to automate installation of Citrix XenServer xe-guest-utilities
  6.  
  7. INITSCRIPT=/etc/init.d/xe-daemon
  8. APP=$(basename $0)
  9.  
  10.  
  11. # Setting up colors & attributes *if* tput exists *and* display supports colors
  12. if which tput &> /dev/null; then
  13.   if (( $(tput colors) >=8 )); then
  14.     c_c="$(tput setaf 6)"   #Cyan
  15.     c_g="$(tput setaf 2)"   #Green
  16.     c_r="$(tput setaf 1)"   #Red
  17.     c_w="$(tput setaf 7)"   #White
  18.     c_b="$(tput setaf 4)"   #Blue
  19.     c_0="$(tput op)"        #Normal
  20.     c__b="$(tput bold)"     #Begin bold
  21.     c__0="$(tput sgr0)"     #End attributes
  22.  
  23.     c_cb="$c_c$c__b"         #Cyan Bold
  24.     c_gb="$c_g$c__b"         #Green Bold
  25.     c_rb="$c_r$c__b"         #Red Bold
  26.     c_wb="$c_w$c__b"         #White BOld
  27.     c_bb="$c_b$c__b"         #Blue Bold
  28.     c_00="$c__0$c_0"         #Normal without attributes
  29.   fi
  30. fi
  31.  
  32. # Standardized help
  33. # The coloring variables will not disturb the output even if display does not
  34. # support colors, because they will be null ""
  35. PrintHelp() {
  36.   echo "
  37. ${c_wb}${APP}${c_00} - XenServer Guest Utilities Installer
  38.  
  39. SYNTAX:
  40.  
  41.    ${c_wb}${APP} ${c_c}PATH ARCH${c_00}
  42.  
  43.    ${c_cb}PATH${c_00} is either one of:
  44.       1* Device where ${c_gb}xs-tools.iso${c_00} is loaded
  45.       2* Path containing the ${c_rb}.rpm${c_00} files
  46.       3* Path containing the ${c_bb}Linux${c_00} directory containing #2 above
  47.  
  48.    ${c_cb}ARCH${c_00} is either one of:
  49.       ${c_cb}i386${c_00} if you need the 32-bit variant, or
  50.       ${c_cb}x86_64${c_00} or ${c_cb}amd64${c_00} if you need the 64-bit variant.
  51.       (The latter two are synonyms)
  52.      
  53. "
  54. }
  55.  
  56.  
  57. # First, check if help is requested. If so, emit help, and exit normally.
  58. if [[ "-h" == "$1" || "--help" == "$1" ]] ; then
  59.   PrintHelp
  60.   exit 0
  61. fi
  62.  
  63. # We need 2 parameters. If the 2nd parameter is null, print help, and exit abnormally
  64. if [[ "" == "$2" ]] ; then
  65.   PrintHelp
  66.   exit 1
  67. fi
  68.  
  69. # Limit allowable ARCH-es
  70. case $2 in
  71.   i386)
  72.     ARCH=$2
  73.     ;;
  74.   x86_64|x86-64|amd64)
  75.     ARCH=x86_64
  76.     ;;
  77.   *)
  78.     PrintHelp
  79.     exit 1
  80.     ;;
  81. esac
  82.  
  83. ProgressReport() {
  84.   printf "\n "
  85.   if [[ $1 == 0 ]]; then
  86.     printf "${c_gb}*${c_00} "
  87.   else
  88.     printf "${c_rb}*${c_00} "
  89.   fi
  90.   printf "$2"
  91. }
  92.  
  93. # Check if necessary tools are installed
  94. ExitIfNotMerged() {
  95.   if ! which $1 &> /dev/null; then
  96.     ProgressReport 1 "Can't find ${c_wb}${1}${c_00}!"
  97.     [[ "$2" ]] && ProgressReport 1 "Have you ${c_cb}emerge ${2}${c_00} yet?\n\n"
  98.     exit 2
  99.   fi
  100. }
  101.  
  102. ExitIfNotMerged rpm2cpio app-arch/rpm
  103. ExitIfNotMerged cpio     app-arch/cpio
  104.  
  105. # Let's process the source first
  106. SRC=$1
  107.  
  108. # First, check if parameter is a block special file (in which case, we must mount)
  109. if [[ -b $SRC ]]; then
  110.   ProgressReport 0 "Mounting $SRC as /mnt"
  111.   mount $SRC /mnt &> /dev/null
  112.   SRCPATH=/mnt
  113. else
  114.   SRCPATH=$SRC
  115. fi
  116.  
  117. # If the SRCPATH is not a directory, then abort
  118. # (if $1 is a device, SRCPATH is the mounted path of the device)
  119. if ! [[ -d $SRCPATH ]]; then
  120.   ProgressReport 1 "${c_bb}$SRCPATH${c_00} is not found. Aborting!\n\n"
  121.   exit 3
  122. fi
  123.  
  124. # Check that there are 2 .rpm files we need here
  125. ProgressReport 0 "Searching for ${c_wb}$ARCH${c_00} .rpm files"
  126. if (( $(find $SRCPATH -maxdepth 2 -name "xe-guest-utilities-*.$ARCH.rpm" | wc -l) < 2 )); then
  127.     ProgressReport 1 "Can't find the needed ${c_rb}.rpm${c_00} files."
  128.     ProgressReport 1 "Please check your parameters.\n\n"
  129.     [[ -b $SRC ]] && umount $SRC
  130.     exit 3
  131. fi
  132.  
  133. # Save our current directory, so we can exit the work directories
  134. PREVDIR=$(pwd)
  135.  
  136. for f in $(find $SRCPATH -maxdepth 2 -name "xe-guest-utilities-*.$ARCH.rpm"); do
  137.   TEMPDIR=$(mktemp -d)
  138.   cd $TEMPDIR
  139.   ProgressReport 0 "Extracting $(basename $f)"
  140.   if ! rpm2cpio $f | cpio -id &> /dev/null ; then
  141.     ProgressReport 1 "Error while extracting ${c_rb}${f}${c_00} !!\n\n"
  142.     exit 4
  143.   fi
  144.   if [[ $f =~ xenstore ]]; then
  145.     DIRstore=$TEMPDIR
  146.   else
  147.     DIRutils=$TEMPDIR
  148.   fi
  149. done
  150.  
  151. ### First, we process the utilities directory
  152. ProgressReport 0 "Processing xe-guest-utilities"
  153. procdirs=( /etc/udev/rules.d /usr )
  154. for d in ${procdirs[@]}; do
  155.   cp -r ${DIRutils}${d}/* $d
  156. done
  157.  
  158. ### Second, we process the xenstore directory
  159. ProgressReport 0 "Processing xe-guest-utilities-xenstore"
  160. cp -r ${DIRstore}/usr/* /usr
  161. ProgressReport 0 "Remaking xenstore symbolic links"
  162. for f in ${DIRstore}/usr/bin/xenstore-* ; do
  163.   bf=$(basename $f)
  164.   rm -f /usr/bin/$bf
  165.   ln -s /usr/bin/xenstore /usr/bin/$bf
  166. done
  167.  
  168. ### Three, we create the proper, OpenRC-compatible initscript
  169. ProgressReport 0 "Creating initscript"
  170. cat - > $INITSCRIPT <<<'#!/sbin/runscript
  171. # Copyright (c) 2011 Pandu E Poluan <pandu@poluan.info>
  172. # Distributed under the terms of the GNU General Public License v2 or newer
  173.  
  174. description="xe-daemon enables the XenServer hypervisor to interrogate some status of the Gentoo DomU VM"
  175. description_start="Starts the xe-daemon"
  176. description_stop="Stops the xe-daemon"
  177.  
  178. depend() {
  179.    need localmount
  180.    after bootmisc
  181. }
  182.  
  183. XE_LINUX_DISTRIBUTION=/usr/sbin/xe-linux-distribution
  184. XE_LINUX_DISTRIBUTION_CACHE=/var/cache/xe-linux-distribution
  185. XE_DAEMON=/usr/sbin/xe-daemon
  186. XE_DAEMON_PIDFILE=/var/run/${SVCNAME}.pid
  187.  
  188. checkxen() {
  189.    if [ ! -x "${XE_LINUX_DISTRIBUTION}" ] ; then
  190.        eend 1 "${SVCNAME}: Could not find ${XE_LINUX_DISTRIBUTION}"
  191.        return 1
  192.    else
  193.        return 0
  194.    fi
  195. }
  196.  
  197. checkdom0() {
  198.    if [ -e /proc/xen/capabilities ] && grep -q control_d /proc/xen/capabilities ; then
  199.      ewarn 1 "${SVCNAME}: Not necessary to run this in dom0"
  200.      return 1
  201.    else
  202.      return 0
  203.    fi
  204. }
  205.  
  206. mountxenfs() {
  207.    local XENFS_RSLT=0
  208.    eindent
  209.    if [ ! -e /proc/xen/xenbus ] ; then
  210.        if [ ! -d /proc/xen ] ; then
  211.            eerror "Could not find /proc/xen directory!"
  212.            eerror "Need a post 2.6.29-rc1 kernel with CONFIG_XEN_COMPAT_XENFS=y and CONFIG_XENFS=y|m"
  213.            XENFS_RSLT=1
  214.        else
  215.            # This is needed post 2.6.29-rc1 when /proc/xen support was pushed upstream as a xen filesystem
  216.            if mount -t xenfs none /proc/xen ; then
  217.                einfo "xenfs mounted on /proc/xen"
  218.            else
  219.                eerror "Failed mounting xenfs on /proc/xen!"
  220.                XENFS_RSLT=1
  221.            fi
  222.        fi
  223.    fi
  224.    eoutdent
  225.    return $XENFS_RSLT
  226. }
  227.  
  228. start() {
  229.    checkxen || return 1
  230.    checkdom0 || return 1
  231.  
  232.    ebegin "${SVCNAME} starting..."
  233.  
  234.    if mountxenfs ; then
  235.      :
  236.    else
  237.      eend 1 "${SVCNAME} not started!"
  238.      return 1
  239.    fi
  240.  
  241.    eindent
  242.  
  243.      einfo "Detecting Linux distribution version"
  244.      ${XE_LINUX_DISTRIBUTION} ${XE_LINUX_DISTRIBUTION_CACHE}
  245.  
  246.      einfo "Daemonizing"
  247.      mkdir -p $(dirname ${XE_DAEMON_PIDFILE})
  248.  
  249.    eoutdent
  250.  
  251.    start-stop-daemon --start --exec "${XE_DAEMON}" --background \
  252.        --pidfile "${XE_DAEMON_PIDFILE}" \
  253.        -- -p ${XE_DAEMON_PIDFILE}
  254.  
  255.    eend $?
  256. }
  257.  
  258. stop() {
  259.    ebegin "Stopping ${SVCNAME}"
  260.    start-stop-daemon --stop --exec "/usr/sbin/xe-daemon" --pidfile "/var/run/xe-daemon.pid"
  261.    eend $?
  262. }
  263.  
  264. ## End of xe-daemon initscript
  265. '
  266. chmod +x $INITSCRIPT
  267.  
  268. ### Four, patch /usr/sbin/xe-linux-distribution
  269. ProgressReport 0 "Patching /usr/sbin/xe-linux-distribution"
  270. cd /usr/sbin
  271. patch <<< "\
  272. --- xe-linux-distribution       2011-07-06 14:24:23.551498005 +0700
  273. +++ xe-linux-distribution.gentoo        2011-07-06 15:18:52.107497524 +0700
  274. @@ -249,6 +249,24 @@
  275.     write_to_output \"\${distro}\" \"\${major}\" \"\${minor}\" \"\${description}\"
  276. }
  277.  
  278. +identify_gentoo()
  279. +{
  280. +       gentoo_release=\"\$1\"
  281. +       if [ ! -e \"\${gentoo_release}\" ] ; then
  282. +               return 1
  283. +       fi
  284. +       distro=\"gentoo\"
  285. +       eval \$(cat \${gentoo_release} | awk '{ print \"release=\" \$5 }' )
  286. +       if [ -z \"\${release}\" ] ; then
  287. +               return 1
  288. +       fi
  289. +       eval \$(echo \$release | awk -F. -- '{ print \"major=\" \$1 ; print \"minor=\" \$2 }' )
  290. +       if [ -z \"\${major}\" -o -z \"\$minor\" ] ; then
  291. +               return 1
  292. +       fi
  293. +       write_to_output \"\${distro}\" \"\${major}\" \"\${minor}\" \"\${distro}\"
  294. +}
  295. +
  296. if [ \$# -eq 1 ] ; then
  297.     exec 1>\"\$1\"
  298. fi
  299. @@ -258,6 +276,7 @@
  300.     identify_sles   /etc/SuSE-release   && exit 0
  301.     identify_lsb    lsb_release         && exit 0
  302.     identify_debian /etc/debian_version && exit 0
  303. +    identify_gentoo /etc/gentoo-release && exit 0
  304.  
  305.     if [ \$# -eq 1 ] ; then
  306.        rm -f \"\$1\"
  307. " &> /dev/null
  308.  
  309. # Clean up
  310. ProgressReport 0 "Cleaning up"
  311. cd $PREVDIR
  312. rm -rf $DIRutils $DIRstore
  313.  
  314. # Unmount if it was a device we're reading from
  315. if [[ -b $SRC ]] ; then
  316.   ProgressReport 0 "Unmounting $SRC"
  317.   umount $SRC
  318. fi
  319.  
  320. printf "\n
  321.   Successfully installed xe-guest-utilities.
  322.   Don't forget to do ${c_cb}rc-update add xe-daemon default${c_00} to ensure that the
  323.   xe-daemon runs on boot.
  324.  
  325.   After doing the ${c_c}rc-update${c_00} above, you are strongly suggested to reboot.
  326.  
  327.   After reboot, you can check on xe-daemon's status by entering the following
  328.   command: ${c_cb}pgrep xe-daemon${c_00}
  329.  
  330. "
  331.  
  332. exit 0
  333.  
  334. ## End of install-xe-guest-utilities.sh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement