aports

Custom Alpine Linux aports

git clone git://git.lin.moe/aports.git

 1#!/bin/sh
 2
 3# Takes care of /dev/bus/usb devices.
 4#
 5# The mdev.conf gives us limited options on how to handle ownership, so a fix has come as script.
 6#
 7# Features:
 8# - Support smartcard devices. Adjust device group to one that let pcscd open them.
 9# - If VirtuaBox is present, support it's usb subsystem by duplicating devices in /dev/vboxusb.
10# - If Android device is plugged in, make it possible for users in plugdev group to open them (mtp, fastboot, adb etc)
11# - Old mdev+kernel used to keep USB devices in root of /dev, relocate them into a place where they are expected..
12
13umask 022
14
15sys_path="/sys/${DEVPATH}"
16vbox_group='vboxusers'
17vbox_device_root='/dev/vboxusb'
18vbox_device_dir="${vbox_device_root}/${BUSNUM}"
19vbox_device_path="${vbox_device_dir}/${DEVNUM}"
20plugdev_group="plugdev"
21# smartcard_group="pcscd"
22smartcard_group="gnupg"
23
24
25case "${ACTION}" in
26    'add')
27        [ "${BUSNUM}" ] || return 1
28        [ "${DEVNUM}" ] || return 1 
29        [ "${MAJOR}" ] || return 1
30        [ "${MINOR}" ] || return 1
31        mkdir -p "/dev/bus/usb/${BUSNUM}"
32
33        # $MDEV will be exported when running `mdev -s`
34        # $DEVNAME and $DEVPATH will be when we poke uevent files with add in init script.
35        if [ "/dev/${MDEV}" != "/dev/bus/usb/${BUSNUM}/${DEVNUM}" ]; then
36            mv "/dev/${MDEV}" "/dev/bus/usb/${BUSNUM}/${DEVNUM}"
37        fi
38
39        if ! [ "${DEVPATH}" ]; then
40            return 1
41        fi
42
43        if [ -f "${sys_path}/product" ]; then
44		idVendor="$(cat ${sys_path}/idVendor)"
45            case "${idVendor}" in
46                # Smartcards (yubikey etc).
47                '1050' | '20a0')
48                    # The tricky part here is that we may run into race conditon with pcscd not picking usb devices if the ownership is wrong.
49                    # the `pcscd --hotplug` does not poke USB devices sadly. We better be fast!
50                    chgrp "${smartcard_group}" "/dev/bus/usb/${BUSNUM}/${DEVNUM}" 2>/dev/null && chmod 660 "/dev/bus/usb/${BUSNUM}/${DEVNUM}"
51                ;;
52                # Android devices.
53                '0bb4'|'18d1'|'22b8'|'0fce'|'19d2'|'04e8'|'2717'|'05c6')
54                chgrp "${plugdev_group}" "/dev/bus/usb/${BUSNUM}/${DEVNUM}" 2>/dev/null && chmod 660 "/dev/bus/usb/${BUSNUM}/${DEVNUM}"
55            ;;
56            esac
57        fi
58
59        if grep -q "${vbox_group}" /etc/group; then
60            install -d "${vbox_device_root}" -g "${vbox_group}" -o 'root' -m 0750
61            install -d "${vbox_device_dir}" -g "${vbox_group}" -o 'root' -m 0750
62            mknod "${vbox_device_path}" c "${MAJOR}" "${MINOR}" -m 0660 2>/dev/null
63            chown "root:${vbox_group}" "${vbox_device_path}"
64        fi
65    ;;
66    'remove')
67        [ "${BUSNUM}" ] || return 1
68        [ "${DEVNUM}" ] || return 1 
69        [ "${MAJOR}" ] || return 1
70        [ "${MINOR}" ] || return 1
71        rm "/dev/bus/usb/${BUSNUM}/${DEVNUM}"
72        rmdir "/dev/bus/usb/${BUSNUM}" 2>/dev/null
73        rmdir "/dev/bus/usb" 2>/dev/null
74
75        if grep -q "${vbox_group}" /etc/group; then
76            rm "${vbox_device_path}"
77            rmdir "${vbox_device_dir}" 2>/dev/null
78        fi
79    ;;
80esac