rhymiz

HP Display Link - Ubuntu/Elementary OS

Oct 21st, 2015
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.99 KB | None | 0 0
  1. #!/bin/bash
  2. # Copyright (c) 2015 DisplayLink (UK) Ltd.
  3.  
  4. SELF=$0
  5. COREDIR=/usr/lib/displaylink
  6. LOGSDIR=/var/log/displaylink
  7. PRODUCT="DisplayLink Linux Software"
  8. VERSION=1.0.138
  9. ACTION=default
  10. SYSTEMINITDAEMON=upstart
  11.  
  12. add_udev_rule()
  13. {
  14.   echo 'ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="17e9", ATTR{bNumInterfaces}=="*5", GROUP="plugdev", MODE="0660"' > /etc/udev/rules.d/99-displaylink.rules
  15.   chmod 0644 /etc/udev/rules.d/99-displaylink.rules
  16. }
  17.  
  18. remove_udev_rule()
  19. {
  20.   rm -f /etc/udev/rules.d/99-displaylink.rules
  21. }
  22.  
  23. install_module()
  24. {
  25.   TARGZ="$1"
  26.   MODVER="$2"
  27.   ERRORS="$3"
  28.  
  29.   SRCDIR="/usr/src/evdi-$MODVER"
  30.   mkdir -p "$SRCDIR"
  31.   if ! tar xf $TARGZ -C "$SRCDIR"; then
  32.     echo "Unable to extract $TARGZ" to "$SRCDIR" > $ERRORS
  33.     return 1
  34.   fi
  35.  
  36.   echo "Registering EVDI kernel module with DKMS"
  37.   dkms add evdi/$MODVER -q
  38.   if [ $? != 0 -a $? != 3 ]; then
  39.     echo "Unable to add evdi/$MODVER to DKMS source tree." > $ERRORS
  40.     return 2
  41.   fi
  42.  
  43.   echo "Building EVDI kernel module with DKMS"
  44.   dkms build evdi/$MODVER -q
  45.   if [ $? != 0 ]; then
  46.     echo "Failed to build evdi/$MODVER. Consult /var/lib/dkms/evdi/$MODVER/build/make.log for details." > $ERRORS
  47.     return 3
  48.   fi
  49.  
  50.   echo "Installing EVDI kernel module to kernel tree"
  51.   dkms install evdi/$MODVER -q
  52.   if [ $? != 0 ]; then
  53.     echo "Failed to install evdi/$MODVER to the kernel tree." > $ERRORS
  54.     return 4
  55.   fi
  56.  
  57.   echo "EVDI kernel module built successfully"
  58. }
  59.  
  60. remove_module()
  61. {
  62.   MODVER="$1"
  63.   SRCDIR="/usr/src/evdi-$MODVER"
  64.   dkms remove evdi/$MODVER --all -q
  65.   [ -d "$SRCDIR" ] && rm -rf "$SRCDIR"
  66. }
  67.  
  68. is_64_bit()
  69. {
  70.   [ "$(getconf LONG_BIT)" == "64" ]
  71. }
  72.  
  73. add_upstart_script()
  74. {
  75.   cat > /etc/init/displaylink.conf <<'EOF'
  76. description "DisplayLink Manager Service"
  77. # Copyright (c) 2015 DisplayLink (UK) Ltd.
  78.  
  79. start on login-session-start
  80. stop on desktop-shutdown
  81.  
  82. # Restart if process crashes
  83. respawn
  84.  
  85. # Only attempt to respawn 10 times in 5 seconds
  86. respawn limit 10 5
  87.  
  88. chdir /usr/lib/displaylink
  89.  
  90. script
  91.     [ -r /etc/default/displaylink ] && . /etc/default/displaylink
  92.     modprobe evdi
  93.     exec /usr/lib/displaylink/DisplayLinkManager
  94. end script
  95. EOF
  96.  
  97.   chmod 0644 /etc/init/displaylink.conf
  98. }
  99.  
  100. add_systemd_service()
  101. {
  102.   cat > /lib/systemd/system/displaylink.service <<'EOF'
  103. [Unit]
  104. Description=DisplayLink Manager Service
  105. After=display-manager.service
  106.  
  107. [Service]
  108. ExecStartPre=/sbin/modprobe evdi
  109. ExecStart=/usr/lib/displaylink/DisplayLinkManager
  110. Restart=always
  111. WorkingDirectory=/usr/lib/displaylink
  112. RestartSec=5
  113.  
  114. [Install]
  115. WantedBy=graphical.target
  116.  
  117. EOF
  118.  
  119.   chmod 0644 /lib/systemd/system/displaylink.service
  120.   systemctl enable displaylink.service
  121. }
  122.  
  123. remove_upstart_script()
  124. {
  125.   rm -f /etc/init/displaylink.conf
  126. }
  127.  
  128. remove_systemd_service()
  129. {
  130.   systemctl disable displaylink.service
  131.   rm -f /lib/systemd/system/displaylink.service
  132. }
  133.  
  134. add_pm_script()
  135. {
  136.   cat > $COREDIR/displaylink.sh <<EOF
  137. #!/bin/bash
  138. # Copyright (c) 2015 DisplayLink (UK) Ltd.
  139.  
  140. suspend_dlm()
  141. {
  142.   #flush any bytes in pipe
  143.   while read -n 1 -t 1 SUSPEND_RESULT < $COREDIR/PmMessagesPort_out; do : ; done;
  144.  
  145.   #suspend DisplayLinkManager
  146.   echo "S" > $COREDIR/PmMessagesPort_in
  147.  
  148.   #wait until suspend of DisplayLinkManager finish
  149.   read -n 1 -t 10 SUSPEND_RESULT < $COREDIR/PmMessagesPort_out
  150. }
  151.  
  152. resume_dlm()
  153. {
  154.   #resume DisplayLinkManager
  155.   echo "R" > $COREDIR/PmMessagesPort_in
  156. }
  157.  
  158. EOF
  159.  
  160.   if [ "$1" = "upstart" ]
  161.   then
  162.     cat >> $COREDIR/displaylink.sh <<EOF
  163. case "\$1" in
  164.   thaw)
  165.     resume_dlm
  166.     ;;
  167.   hibernate)
  168.     suspend_dlm
  169.     ;;
  170.   suspend)
  171.     suspend_dlm
  172.     ;;
  173.   resume)
  174.     resume_dlm
  175.     ;;
  176. esac
  177.  
  178. EOF
  179.   elif [ "$1" = "systemd" ]
  180.   then
  181.     cat >> $COREDIR/displaylink.sh <<EOF
  182. case "\$1/\$2" in
  183.   pre/*)
  184.     suspend_dlm
  185.     ;;
  186.   post/*)
  187.     resume_dlm
  188.     ;;
  189. esac
  190.  
  191. EOF
  192.   fi
  193.  
  194.   chmod 0755 $COREDIR/displaylink.sh
  195.   if [ "$1" = "upstart" ]
  196.   then
  197.     ln -s $COREDIR/displaylink.sh /etc/pm/sleep.d/displaylink.sh
  198.   elif [ "$1" = "systemd" ]
  199.   then
  200.     ln -s $COREDIR/displaylink.sh /lib/systemd/system-sleep/displaylink.sh
  201.   fi
  202. }
  203.  
  204. remove_pm_scripts()
  205. {
  206.   rm -f /etc/pm/sleep.d/displaylink.sh
  207.   rm -f /lib/systemd/system-sleep/displaylink.sh
  208. }
  209.  
  210. cleanup()
  211. {
  212.   rm -rf $COREDIR
  213.   rm -rf $LOGSDIR
  214.   rm -f /usr/bin/displaylink-installer
  215. }
  216.  
  217. install()
  218. {
  219.   echo "Installing"
  220.   mkdir -p $COREDIR
  221.   mkdir -p $LOGSDIR
  222.   chmod 0755 $COREDIR
  223.   chmod 0755 $LOGSDIR
  224.  
  225.   cp -f $SELF $COREDIR
  226.   ln -sf "$COREDIR/$(basename $SELF)" /usr/bin/displaylink-installer
  227.  
  228.   local ERRORS=$(mktemp)
  229.   echo "Configuring EVDI DKMS module"
  230.   install_module "evdi-$VERSION-src.tar.gz" "$VERSION" "$ERRORS"
  231.   local success=$?
  232.  
  233.   local error="$(< $ERRORS)"
  234.   rm -f $ERRORS
  235.   if [ 0 -ne $success ]; then
  236.     echo "ERROR (code $success): $error." >&2
  237.     cleanup
  238.     exit 1
  239.   fi
  240.  
  241.   is_64_bit && ARCH="x64" || ARCH="x86"
  242.   local DLM="$ARCH/DisplayLinkManager"
  243.   echo "Installing $DLM"
  244.   [ -x $DLM ] && mv -f $DLM $COREDIR
  245.  
  246.   echo "Installing libraries"
  247.   local LIBEVDI="$ARCH/libevdi.so"
  248.   local LIBUSB="$ARCH/libusb-1.0.so.0.1.0"
  249.  
  250.   [ -f $LIBEVDI ] && mv -f $LIBEVDI $COREDIR
  251.   [ -f $LIBUSB ] && mv -f $LIBUSB $COREDIR
  252.   ln -sf $COREDIR/libusb-1.0.so.0.1.0 $COREDIR/libusb-1.0.so.0
  253.   ln -sf $COREDIR/libusb-1.0.so.0.1.0 $COREDIR/libusb-1.0.so
  254.  
  255.   chmod 0755 $COREDIR/DisplayLinkManager
  256.   chmod 0755 $COREDIR/libevdi.so
  257.   chmod 0755 $COREDIR/libusb*.so*
  258.  
  259.   echo "Installing firmware packages"
  260.   mv -f *.spkg $COREDIR
  261.   chmod 0644 $COREDIR/*.spkg
  262.  
  263.   echo "Installing license file"
  264.   mv -f LICENSE $COREDIR
  265.   chmod 0644 $COREDIR/LICENSE
  266.  
  267.   echo "Adding udev rule for DisplayLink DL-3xxx/5xxx devices"
  268.   add_udev_rule
  269.  
  270.   if [ "upstart" == "$SYSTEMINITDAEMON" ]; then
  271.     echo "Starting DLM upstart job"
  272.     add_upstart_script
  273.     add_pm_script "upstart"
  274.     start displaylink
  275.   elif [ "systemd" == "$SYSTEMINITDAEMON" ]; then
  276.     echo "Starting DLM systemd service"
  277.     add_systemd_service
  278.     add_pm_script "systemd"
  279.     systemctl start displaylink.service
  280.   fi
  281. }
  282.  
  283. uninstall()
  284. {
  285.   echo "Uninstalling"
  286.  
  287.   echo "Removing EVDI from kernel tree, DKMS, and removing sources."
  288.   remove_module $VERSION
  289.  
  290.   if [ "upstart" == "$SYSTEMINITDAEMON" ]; then
  291.     echo "Stopping DLM upstart job"
  292.     stop displaylink
  293.     remove_upstart_script
  294.   elif [ "systemd" == "$SYSTEMINITDAEMON" ]; then
  295.     echo "Stopping DLM systemd service"
  296.     systemctl stop displaylink.service
  297.     remove_systemd_service
  298.   fi
  299.  
  300.   echo "Removing suspend-resume hooks"
  301.   remove_pm_scripts
  302.  
  303.   echo "Removing udev rule"
  304.   remove_udev_rule
  305.  
  306.   echo "Removing Core folder"
  307.   cleanup
  308.  
  309.   echo -e "\nUninstallation steps complete."
  310.   if [ -f /sys/devices/evdi/version ]; then
  311.     echo "Please note that the evdi kernel module is still in the memory."
  312.     echo "A reboot is required to fully complete the uninstallation process."
  313.   fi
  314. }
  315.  
  316. missing_requirement()
  317. {
  318.   echo "Unsatisfied dependencies. Missing component: $1." >&2
  319.   echo "This is a fatal error, cannot install $PRODUCT." >&2
  320.   exit 1
  321. }
  322.  
  323. version_lt()
  324. {
  325.   local left=$(echo $1 | cut -d. -f-2)
  326.   local right=$(echo $2 | cut -d. -f-2)
  327.   local greater=$(echo -e "$left\n$right" | sort -Vr | head -1)
  328.   [ "$greater" != "$left" ] && return $true
  329.   return $false
  330. }
  331.  
  332. check_requirements()
  333. {
  334.   # DKMS
  335.   which dkms >/dev/null || missing_requirement "DKMS"
  336.  
  337.   # Required kernel version
  338.   MIN_KVER="3.14"
  339.   MAX_KVER="3.19"
  340.   KVER=$(uname -r)
  341.  
  342.   version_lt $KVER $MIN_KVER &&
  343.     missing_requirement "Kernel version $KVER is too old. At least $MIN_KVER is required"
  344.   version_lt $MAX_KVER $KVER &&
  345.      echo "WARNING: Kernel version $KVER is not supported. Highest supported version is $MAX_KVER."
  346.  
  347.  
  348.   # Required kernel version
  349.   # Thanks to Chris Billington for this patch
  350.   KVER=$(uname -r)
  351.   [ $(echo $KVER | cut -d. -f1) != 3 ] && missing_requirement "Kernel version $KVER is too old. At least 3.14 is required"
  352.   [ $(echo $KVER | cut -d. -f2) -lt 14 ] && missing_requirement "Kernel version $KVER is too old. At least 3.14 is required"
  353.   KMAJVER=$(echo $KVER | cut -d. -f1)
  354.   KMINVER=$(echo $KVER | cut -d. -f2)
  355.   ([ $KMAJVER -lt 3 ] || [ $KMAJVER -eq 3 ] && [ $KMINVER -lt 14 ]) && missing_requirement \
  356.   "Kernel version $KVER is too old. At least 3.14 is required"
  357.    
  358.   # Linux headers
  359.   [ ! -f "/lib/modules/$KVER/build/Kbuild" ] && missing_requirement "Linux headers for running kernel, $KVER"
  360.  
  361. }
  362.  
  363. usage()
  364. {
  365.   echo
  366.   echo "Installs $PRODUCT, version $VERSION."
  367.   echo "Usage: $SELF [ install | uninstall ]"
  368.   echo
  369.   echo "If no argument is given, a quick compatibility check is performed but nothing is installed."
  370.   exit 1
  371. }
  372.  
  373. detect_distro()
  374. {
  375.   if which lsb_release >/dev/null; then
  376.     local R=$(lsb_release -d -s)
  377.     echo "Distribution discovered: $R"
  378.     if [ -z "${R##Ubuntu 14.*}" ]; then
  379.       SYSTEMINITDAEMON=upstart
  380.     elif [ -z "${R##Ubuntu 15.04*}" ]; then
  381.       SYSTEMINITDAEMON=systemd
  382.     fi
  383.   else
  384.     echo "WARNING: Unknown distribution, assuming defaults - this may fail." >&2
  385.   fi
  386. }
  387.  
  388. ensure_not_running()
  389. {
  390.   if [ -f /sys/devices/evdi/version ]; then
  391.     local V=$(< /sys/devices/evdi/version)
  392.     echo "WARNING: Version $V of EVDI kernel module is already running." >&2
  393.     if [ -d $COREDIR ]; then
  394.       echo "Please uninstall all other versions of $PRODUCT before attempting to install." >&2
  395.     else
  396.       echo "Please reboot before attempting to re-install $PRODUCT." >&2
  397.     fi
  398.     echo "Installation terminated." >&2
  399.     exit 1
  400.   fi
  401. }
  402.  
  403. if [ $(id -u) != 0 ]; then
  404.   echo "You need to be root to use this script." >&2
  405.   exit 1
  406. fi
  407.  
  408. echo "$PRODUCT $VERSION install script called: $*"
  409. detect_distro
  410. check_requirements
  411.  
  412. while [ -n "$1" ]; do
  413.   case "$1" in
  414.     install)
  415.       ACTION="install"
  416.       ;;
  417.  
  418.     uninstall)
  419.       ACTION="uninstall"
  420.       ;;
  421.  
  422.     *)
  423.       usage
  424.       ;;
  425.   esac
  426.   shift
  427. done
  428.  
  429. if [ "$ACTION" == "install" ]; then
  430.   ensure_not_running
  431.   install
  432. elif [ "$ACTION" == "uninstall" ]; then
  433.   uninstall
  434. fi
Advertisement
Add Comment
Please, Sign In to add comment