VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/installer-utils.sh@ 39121

Last change on this file since 39121 was 38385, checked in by vboxsync, 14 years ago

Installer/Linux: cleaned up the USB device node installation for the generic installer

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1# Oracle VM VirtualBox
2# VirtualBox installer shell routines
3#
4
5# Copyright (C) 2007-2011 Oracle Corporation
6#
7# This file is part of VirtualBox Open Source Edition (OSE), as
8# available from http://www.virtualbox.org. This file is free software;
9# you can redistribute it and/or modify it under the terms of the GNU
10# General Public License (GPL) as published by the Free Software
11# Foundation, in version 2 as it comes in the "COPYING" file of the
12# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14#
15
16setup_normal_input_install_udev() {
17 # The udev rule setup functions contain some aliases which are used for unit
18 # testing purposes. This function sets up the aliases to point to the real
19 # commands for "normal" use.
20 eval 'my_which() { which "$@" ; }'
21 eval 'my_test() { test "$@" ; }'
22 eval 'my_rm() { rm "$@" ; }'
23}
24
25# Needs to be done before the udev setup functions are called "normally" (i.e.
26# not in a unit testing context). Will be overridden later in a unit test.
27setup_normal_input_install_udev
28
29setup_test_input_install_udev() {
30 # Set up unit testing environment for the "install_udev" function below.
31 TEST_NAME="$1" # used to identify the current test
32 TEST_UDEV_VERSION="$2" # udev version to simulate
33 eval 'my_which() { echo test_udev ; }'
34 eval 'my_test() { true ; }'
35 eval 'my_rm() { case "$2" in "/etc/udev/rules.d/60-vboxdrv.rules") true ;; *) echo "rm: bad file name \"$2\"!"; false ;; esac ; }'
36 eval 'test_udev() { echo "$TEST_UDEV_VERSION" ; }'
37 DELETED_UDEV_FILE=""
38}
39
40cleanup_test_input_install_udev() {
41 # Clean up after unit testing install_udev for a given input
42 setup_normal_input_install_udev
43 unset test_udev
44 DELETED_UDEV_FILE=""
45}
46
47udev_write_vboxdrv() {
48 VBOXDRV_GRP="$1"
49 VBOXDRV_MODE="$2"
50
51 echo "KERNEL==\"vboxdrv\", NAME=\"vboxdrv\", OWNER=\"root\", GROUP=\"$VBOXDRV_GRP\", MODE=\"$VBOXDRV_MODE\""
52}
53
54udev_write_usb() {
55 INSTALLATION_DIR="$1"
56 USB_GROUP="$2"
57
58 echo "SUBSYSTEM==\"usb_device\", ACTION==\"add\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh \$major \$minor \$attr{bDeviceClass}${USB_GROUP}\""
59 echo "SUBSYSTEM==\"usb\", ACTION==\"add\", ENV{DEVTYPE}==\"usb_device\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh \$major \$minor \$attr{bDeviceClass}${USB_GROUP}\""
60 echo "SUBSYSTEM==\"usb_device\", ACTION==\"remove\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh --remove \$major \$minor\""
61 echo "SUBSYSTEM==\"usb\", ACTION==\"remove\", ENV{DEVTYPE}==\"usb_device\", RUN+=\"$INSTALLATION_DIR/VBoxCreateUSBNode.sh --remove \$major \$minor\""
62}
63
64install_udev() {
65 # install udev rule (disable with INSTALL_NO_UDEV=1 in /etc/default/virtualbox) for distribution packages
66 VBOXDRV_GRP="$1" # The group owning the vboxdrv device
67 VBOXDRV_MODE="$2" # The access mode for the vboxdrv device
68 INSTALLATION_DIR="$3" # The directory VirtualBox is installed in
69 USB_GROUP="$4" # The group that has permission to access USB devices
70 NO_INSTALL="$5" # Set this to "1" to remove but not re-install rules
71
72 # Extra space!
73 case "$USB_GROUP" in ?*) USB_GROUP=" $USB_GROUP" ;; esac
74 case "$NO_INSTALL" in
75 "1") ;;
76 *)
77 if my_test -d /etc/udev/rules.d; then
78 udev_call=""
79 udev_app=`my_which udevadm 2> /dev/null`
80 if [ $? -eq 0 ]; then
81 udev_call="${udev_app} version 2> /dev/null"
82 else
83 udev_app=`my_which udevinfo 2> /dev/null`
84 if [ $? -eq 0 ]; then
85 udev_call="${udev_app} -V 2> /dev/null"
86 fi
87 fi
88 udev_fix=""
89 if [ "${udev_call}" != "" ]; then
90 udev_out=`${udev_call}`
91 udev_ver=`expr "$udev_out" : '[^0-9]*\([0-9]*\)'`
92 if [ "$udev_ver" = "" -o "$udev_ver" -lt 55 ]; then
93 udev_fix="1"
94 fi
95 udev_do_usb=""
96 if [ "$udev_ver" -ge 59 ]; then
97 udev_do_usb="1"
98 fi
99 fi
100 case "$udev_fix" in
101 "1")
102 udev_write_vboxdrv "$VBOXDRV_GRP" "$VBOXDRV_MODE" |
103 sed 's/\([^+=]*\)[+=]*\([^"]*"[^"]*"\)/\1=\2/g'
104 ;;
105 *)
106 udev_write_vboxdrv "$VBOXDRV_GRP" "$VBOXDRV_MODE"
107 case "$udev_do_usb" in "1")
108 udev_write_usb "$INSTALLATION_DIR" "$USB_GROUP" ;;
109 esac
110 ;;
111 esac
112
113 fi
114 ;;
115 esac
116 # Remove old udev description file
117 if my_test -f /etc/udev/rules.d/60-vboxdrv.rules; then
118 my_rm -f /etc/udev/rules.d/60-vboxdrv.rules 2> /dev/null
119 fi
120}
121
122setup_normal_input_install_create_usb_node() {
123 # install_create_usb_node_for_sysfs contain some aliases for unit testing
124 # purposes. This function sets up the aliases to point to the real
125 # commands for "normal" use.
126 eval 'my_test() { test "$@" ; }'
127 eval 'my_cat() { cat "$@" ; }'
128 eval 'my_sh() { sh "$@" ; }'
129}
130
131# Assume we will be calling the function normally, not for unit testing
132setup_normal_input_install_create_usb_node
133
134# Add a unit test here if/when needed following the same pattern as
135# setup_test_input_install_udev.
136
137install_create_usb_node_for_sysfs() {
138 # Create a usb device node for a given sysfs path
139 path="$1" # sysfs path for the device
140 usb_createnode="$2" # Path to the USB device node creation script
141 usb_group="$3" # The group to give ownership of the node to
142 if my_test -r "${path}/dev"; then
143 dev="`my_cat "${path}/dev" 2> /dev/null`"
144 major="`expr "$dev" : '\(.*\):' 2> /dev/null`"
145 minor="`expr "$dev" : '.*:\(.*\)' 2> /dev/null`"
146 class="`my_cat ${path}/bDeviceClass 2> /dev/null`"
147 my_sh "${usb_createnode}" "$major" "$minor" "$class" "${usb_group}" \
148 2>/dev/null
149 fi
150}
151
152setup_normal_input_install_device_node_setup() {
153 # install_device_node_setup contain some aliases for unit testing
154 # purposes. This function sets up the aliases to point to the real
155 # values for "normal" use.
156 udev_rule_file=/etc/udev/rules.d/10-vboxdrv.rules # Set this to /dev/null
157 # for unit testing
158 sysfs_usb_devices="/sys/bus/usb/devices/*"
159 eval 'do_install_udev() { install_udev "$@"; }'
160 eval 'do_install_create_usb_node_for_sysfs() { \
161 install_create_usb_node_for_sysfs "$@" ; }'
162}
163
164# Assume we will be calling the function normally, not for unit testing
165setup_normal_input_install_device_node_setup
166
167setup_test_input_install_device_node_setup() {
168 # Set up unit testing environment for the "install_udev" function below.
169 test_drv_grp="$1" # The expected vboxdrv group
170 test_drv_mode="$2" # The expected vboxdrv mode
171 test_inst_dir="$3" # The expected installation directory
172 test_usb_grp="$4" # The expected USB node group
173 udev_rule_file=/dev/null
174 sysfs_usb_devices=test_sysfs_path
175 eval 'do_install_udev() { test "$1" = "${test_drv_grp}" \
176 -a "$2" = "${test_drv_mode}" \
177 -a "$3" = "${test_inst_dir}" \
178 -a "$4" = "${test_usb_grp}" \
179 -a "$5" = "${INSTALL_NO_UDEV}" \
180 || echo "do_install_udev: bad parameters: $@" >&2 ; }'
181 eval 'do_install_create_usb_node_for_sysfs() { \
182 test "$1" = "${sysfs_usb_devices}" \
183 -a "$2" = "${test_inst_dir}/VBoxCreateUSBNode.sh" \
184 -a "$3" = "${test_usb_grp}" \
185 || echo "do_install_create_usb_node_for_sysfs: \
186bad parameters: $@" >&2 ; }'
187}
188
189cleanup_test_input_install_device_node_setup() {
190 # Clean up unit testing environment for the "install_udev" function below.
191 # Nothing to do.
192 true
193}
194
195install_device_node_setup() {
196 # Install udev rules and create device nodes for usb access
197 VBOXDRV_GRP="$1" # The group that should own /dev/vboxdrv
198 VBOXDRV_MODE="$2" # The mode to be used for /dev/vboxdrv
199 INSTALLATION_DIR="$3" # The directory VirtualBox is installed in
200 USB_GROUP="$4" # The group that should own the /dev/vboxusb device
201 # nodes unless INSTALL_NO_GROUP=1 in
202 # /etc/default/virtualbox. Optional.
203 usb_createnode="$INSTALLATION_DIR/VBoxCreateUSBNode.sh"
204 # install udev rule (disable with INSTALL_NO_UDEV=1 in
205 # /etc/default/virtualbox)
206 if [ "$INSTALL_NO_GROUP" != "1" ]; then
207 usb_group=$USB_GROUP
208 vboxdrv_group=$VBOXDRV_GRP
209 else
210 usb_group=root
211 vboxdrv_group=root
212 fi
213 do_install_udev "${vboxdrv_group}" "$VBOXDRV_MODE" "$INSTALLATION_DIR" \
214 "${usb_group}" "$INSTALL_NO_UDEV" > ${udev_rule_file}
215 # Build our device tree
216 for i in ${sysfs_usb_devices}; do # This line intentionally without quotes.
217 do_install_create_usb_node_for_sysfs "$i" "${usb_createnode}" \
218 "${usb_group}"
219 done
220}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette