Advertisement
rhymiz

HP Display Link - Ubuntu/Elementary OS

Oct 21st, 2015
764
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. Conflicts=getty@tty7.service
  107.  
  108. [Service]
  109. ExecStartPre=/sbin/modprobe evdi
  110. ExecStart=/usr/lib/displaylink/DisplayLinkManager
  111. Restart=always
  112. WorkingDirectory=/usr/lib/displaylink
  113. RestartSec=5
  114.  
  115. [Install]
  116. WantedBy=graphical.target
  117.  
  118. EOF
  119.  
  120.   chmod 0644 /lib/systemd/system/displaylink.service
  121.   systemctl enable displaylink.service
  122. }
  123.  
  124. remove_upstart_script()
  125. {
  126.   rm -f /etc/init/displaylink.conf
  127. }
  128.  
  129. remove_systemd_service()
  130. {
  131.   systemctl disable displaylink.service
  132.   rm -f /lib/systemd/system/displaylink.service
  133. }
  134.  
  135. add_pm_script()
  136. {
  137.   cat > $COREDIR/displaylink.sh <<EOF
  138. #!/bin/bash
  139. # Copyright (c) 2015 DisplayLink (UK) Ltd.
  140.  
  141. suspend_dlm()
  142. {
  143.   #flush any bytes in pipe
  144.   while read -n 1 -t 1 SUSPEND_RESULT < $COREDIR/PmMessagesPort_out; do : ; done;
  145.  
  146.   #suspend DisplayLinkManager
  147.   echo "S" > $COREDIR/PmMessagesPort_in
  148.  
  149.   #wait until suspend of DisplayLinkManager finish
  150.   read -n 1 -t 10 SUSPEND_RESULT < $COREDIR/PmMessagesPort_out
  151. }
  152.  
  153. resume_dlm()
  154. {
  155.   #resume DisplayLinkManager
  156.   echo "R" > $COREDIR/PmMessagesPort_in
  157. }
  158.  
  159. EOF
  160.  
  161.   if [ "$1" = "upstart" ]
  162.   then
  163.     cat >> $COREDIR/displaylink.sh <<EOF
  164. case "\$1" in
  165.   thaw)
  166.     resume_dlm
  167.     ;;
  168.   hibernate)
  169.     suspend_dlm
  170.     ;;
  171.   suspend)
  172.     suspend_dlm
  173.     ;;
  174.   resume)
  175.     resume_dlm
  176.     ;;
  177. esac
  178.  
  179. EOF
  180.   elif [ "$1" = "systemd" ]
  181.   then
  182.     cat >> $COREDIR/displaylink.sh <<EOF
  183. case "\$1/\$2" in
  184.   pre/*)
  185.     suspend_dlm
  186.     ;;
  187.   post/*)
  188.     resume_dlm
  189.     ;;
  190. esac
  191.  
  192. EOF
  193.   fi
  194.  
  195.   chmod 0755 $COREDIR/displaylink.sh
  196.   if [ "$1" = "upstart" ]
  197.   then
  198.     ln -s $COREDIR/displaylink.sh /etc/pm/sleep.d/displaylink.sh
  199.   elif [ "$1" = "systemd" ]
  200.   then
  201.     ln -s $COREDIR/displaylink.sh /lib/systemd/system-sleep/displaylink.sh
  202.   fi
  203. }
  204.  
  205. remove_pm_scripts()
  206. {
  207.   rm -f /etc/pm/sleep.d/displaylink.sh
  208.   rm -f /lib/systemd/system-sleep/displaylink.sh
  209. }
  210.  
  211. cleanup()
  212. {
  213.   rm -rf $COREDIR
  214.   rm -rf $LOGSDIR
  215.   rm -f /usr/bin/displaylink-installer
  216. }
  217.  
  218. install()
  219. {
  220.   echo "Installing"
  221.   mkdir -p $COREDIR
  222.   mkdir -p $LOGSDIR
  223.   chmod 0755 $COREDIR
  224.   chmod 0755 $LOGSDIR
  225.  
  226.   cp -f $SELF $COREDIR
  227.   ln -sf "$COREDIR/$(basename $SELF)" /usr/bin/displaylink-installer
  228.  
  229.   local ERRORS=$(mktemp)
  230.   echo "Configuring EVDI DKMS module"
  231.   install_module "evdi-$VERSION-src.tar.gz" "$VERSION" "$ERRORS"
  232.   local success=$?
  233.  
  234.   local error="$(< $ERRORS)"
  235.   rm -f $ERRORS
  236.   if [ 0 -ne $success ]; then
  237.     echo "ERROR (code $success): $error." >&2
  238.     cleanup
  239.     exit 1
  240.   fi
  241.  
  242.   is_64_bit && ARCH="x64" || ARCH="x86"
  243.   local DLM="$ARCH/DisplayLinkManager"
  244.   echo "Installing $DLM"
  245.   [ -x $DLM ] && mv -f $DLM $COREDIR
  246.  
  247.   echo "Installing libraries"
  248.   local LIBEVDI="$ARCH/libevdi.so"
  249.   local LIBUSB="$ARCH/libusb-1.0.so.0.1.0"
  250.  
  251.   [ -f $LIBEVDI ] && mv -f $LIBEVDI $COREDIR
  252.   [ -f $LIBUSB ] && mv -f $LIBUSB $COREDIR
  253.   ln -sf $COREDIR/libusb-1.0.so.0.1.0 $COREDIR/libusb-1.0.so.0
  254.   ln -sf $COREDIR/libusb-1.0.so.0.1.0 $COREDIR/libusb-1.0.so
  255.  
  256.   chmod 0755 $COREDIR/DisplayLinkManager
  257.   chmod 0755 $COREDIR/libevdi.so
  258.   chmod 0755 $COREDIR/libusb*.so*
  259.  
  260.   echo "Installing firmware packages"
  261.   mv -f *.spkg $COREDIR
  262.   chmod 0644 $COREDIR/*.spkg
  263.  
  264.   echo "Installing license file"
  265.   mv -f LICENSE $COREDIR
  266.   chmod 0644 $COREDIR/LICENSE
  267.  
  268.   echo "Adding udev rule for DisplayLink DL-3xxx/5xxx devices"
  269.   add_udev_rule
  270.  
  271.   if [ "upstart" == "$SYSTEMINITDAEMON" ]; then
  272.     echo "Starting DLM upstart job"
  273.     add_upstart_script
  274.     add_pm_script "upstart"
  275.     start displaylink
  276.   elif [ "systemd" == "$SYSTEMINITDAEMON" ]; then
  277.     echo "Starting DLM systemd service"
  278.     add_systemd_service
  279.     add_pm_script "systemd"
  280.     systemctl start displaylink.service
  281.   fi
  282. }
  283.  
  284. uninstall()
  285. {
  286.   echo "Uninstalling"
  287.  
  288.   echo "Removing EVDI from kernel tree, DKMS, and removing sources."
  289.   remove_module $VERSION
  290.  
  291.   if [ "upstart" == "$SYSTEMINITDAEMON" ]; then
  292.     echo "Stopping DLM upstart job"
  293.     stop displaylink
  294.     remove_upstart_script
  295.   elif [ "systemd" == "$SYSTEMINITDAEMON" ]; then
  296.     echo "Stopping DLM systemd service"
  297.     systemctl stop displaylink.service
  298.     remove_systemd_service
  299.   fi
  300.  
  301.   echo "Removing suspend-resume hooks"
  302.   remove_pm_scripts
  303.  
  304.   echo "Removing udev rule"
  305.   remove_udev_rule
  306.  
  307.   echo "Removing Core folder"
  308.   cleanup
  309.  
  310.   echo -e "\nUninstallation steps complete."
  311.   if [ -f /sys/devices/evdi/version ]; then
  312.     echo "Please note that the evdi kernel module is still in the memory."
  313.     echo "A reboot is required to fully complete the uninstallation process."
  314.   fi
  315. }
  316.  
  317. missing_requirement()
  318. {
  319.   echo "Unsatisfied dependencies. Missing component: $1." >&2
  320.   echo "This is a fatal error, cannot install $PRODUCT." >&2
  321.   exit 1
  322. }
  323.  
  324. version_lt()
  325. {
  326.   local left=$(echo $1 | cut -d. -f-2)
  327.   local right=$(echo $2 | cut -d. -f-2)
  328.   local greater=$(echo -e "$left\n$right" | sort -Vr | head -1)
  329.   [ "$greater" != "$left" ] && return $true
  330.   return $false
  331. }
  332.  
  333. check_requirements()
  334. {
  335.   # DKMS
  336.   which dkms >/dev/null || missing_requirement "DKMS"
  337.  
  338.   # Required kernel version
  339.   MIN_KVER="3.14"
  340.   MAX_KVER="3.19"
  341.   KVER=$(uname -r)
  342.  
  343.   version_lt $KVER $MIN_KVER &&
  344.     missing_requirement "Kernel version $KVER is too old. At least $MIN_KVER is required"
  345.   version_lt $MAX_KVER $KVER &&
  346.      echo "WARNING: Kernel version $KVER is not supported. Highest supported version is $MAX_KVER."
  347.  
  348.  
  349.   # Required kernel version
  350.   # Thanks to Chris Billington for this patch
  351.   KVER=$(uname -r)
  352.   [ $(echo $KVER | cut -d. -f1) != 3 ] && missing_requirement "Kernel version $KVER is too old. At least 3.14 is required"
  353.   [ $(echo $KVER | cut -d. -f2) -lt 14 ] && missing_requirement "Kernel version $KVER is too old. At least 3.14 is required"
  354.   KMAJVER=$(echo $KVER | cut -d. -f1)
  355.   KMINVER=$(echo $KVER | cut -d. -f2)
  356.   ([ $KMAJVER -lt 3 ] || [ $KMAJVER -eq 3 ] && [ $KMINVER -lt 14 ]) && missing_requirement \
  357.   "Kernel version $KVER is too old. At least 3.14 is required"
  358.    
  359.   # Linux headers
  360.   [ ! -f "/lib/modules/$KVER/build/Kbuild" ] && missing_requirement "Linux headers for running kernel, $KVER"
  361.  
  362. }
  363.  
  364. usage()
  365. {
  366.   echo
  367.   echo "Installs $PRODUCT, version $VERSION."
  368.   echo "Usage: $SELF [ install | uninstall ]"
  369.   echo
  370.   echo "If no argument is given, a quick compatibility check is performed but nothing is installed."
  371.   exit 1
  372. }
  373.  
  374. detect_distro()
  375. {
  376.   if which lsb_release >/dev/null; then
  377.     local R=$(lsb_release -d -s)
  378.     echo "Distribution discovered: $R"
  379.     if [ -z "${R##Ubuntu 14.*}" ]; then
  380.       SYSTEMINITDAEMON=upstart
  381.     elif [ -z "${R##Ubuntu 15.04*}" ]; then
  382.       SYSTEMINITDAEMON=systemd
  383.     fi
  384.   else
  385.     echo "WARNING: Unknown distribution, assuming defaults - this may fail." >&2
  386.   fi
  387. }
  388.  
  389. ensure_not_running()
  390. {
  391.   if [ -f /sys/devices/evdi/version ]; then
  392.     local V=$(< /sys/devices/evdi/version)
  393.     echo "WARNING: Version $V of EVDI kernel module is already running." >&2
  394.     if [ -d $COREDIR ]; then
  395.       echo "Please uninstall all other versions of $PRODUCT before attempting to install." >&2
  396.     else
  397.       echo "Please reboot before attempting to re-install $PRODUCT." >&2
  398.     fi
  399.     echo "Installation terminated." >&2
  400.     exit 1
  401.   fi
  402. }
  403.  
  404. if [ $(id -u) != 0 ]; then
  405.   echo "You need to be root to use this script." >&2
  406.   exit 1
  407. fi
  408.  
  409. echo "$PRODUCT $VERSION install script called: $*"
  410. detect_distro
  411. check_requirements
  412.  
  413. while [ -n "$1" ]; do
  414.   case "$1" in
  415.     install)
  416.       ACTION="install"
  417.       ;;
  418.  
  419.     uninstall)
  420.       ACTION="uninstall"
  421.       ;;
  422.  
  423.     *)
  424.       usage
  425.       ;;
  426.   esac
  427.   shift
  428. done
  429.  
  430. if [ "$ACTION" == "install" ]; then
  431.   ensure_not_running
  432.   install
  433. elif [ "$ACTION" == "uninstall" ]; then
  434.   uninstall
  435. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement