Advertisement
pepoluan

install-xe-guest-utilities.sh for Gentoo

Jul 6th, 2011
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.84 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. # Check if necessary tools are installed
  84. ExitIfNotMerged() {
  85.   if ! which $1 &> /dev/null; then
  86.     printf "\nCan't find ${c_wb}${1}${c_00}."
  87.     [[ "$2" ]] && printf "\nHave you ${c_cb}emerge ${2}${c_00} yet?\n\n"
  88.     exit 2
  89.   fi
  90. }
  91.  
  92. ExitIfNotMerged rpm2cpio app-arch/rpm
  93. ExitIfNotMerged cpio     app-arch/cpio
  94.  
  95. ProgressReport() {
  96.   printf "\n "
  97.   if [[ $1 == 0 ]]; then
  98.     printf "${c_gb}*${c_00} "
  99.   else
  100.     printf "${c_rb}*${c_00} "
  101.   fi
  102.   printf "$2"
  103. }
  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.   printf "\n${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 "\nCan't find the needed ${c_rb}.rpm${c_00} files."
  128.     ProgressReport 1 "\nPlease 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} !!"
  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 -rn ${DIRutils}${d}/* $d
  156. done
  157.  
  158. ### Second, we process the xenstore directory
  159. ProgressReport 0 "Processing xe-guest-utilities-xenstore"
  160. cp -rn ${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. # Clean up
  269. ProgressReport 0 "Cleaning up"
  270. cd $PREVDIR
  271. rm -rf $DIRutils $DIRstore
  272.  
  273. # Unmount if it was a device we're reading from
  274. if [[ -b $SRC ]] ; then
  275.   ProgressReport 0 "Unmounting $SRC"
  276.   umount $SRC
  277. fi
  278.  
  279. printf "\n
  280.   Successfully installed xe-guest-utilities.
  281.   Don't forget to do ${c_cb}rc-update add xe-daemon default${c_00} to ensure that the
  282.   xe-daemon runs on boot.
  283.  
  284.   After doing the ${c_c}rc-update${c_00} above, you are strongly suggested to reboot.
  285.  
  286.   After reboot, you can check on xe-daemon's status by entering the following
  287.   command: ${c_cb}pgrep xe-daemon${c_00}
  288. "
  289.  
  290. exit 0
  291.  
  292. ## End of install-xe-guest-utilities.sh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement