\section{udev and Device Management}

\begin{frame}
   {udev}
   \begin{itemize}
      \item \textbf{udev} stands for \textbf{U}ser \textbf{Dev}ice management

      \item \textbf{udev} dynamically discovers
      built in hardware as well as peripheral devices:
      \begin{itemize}
         \item during system boot
         \item when \textbf{hotplugged} at any time
      \end{itemize}
      \item \textbf{udev} handles loading and unloading device
      drivers with proper configurations as need
      \item \textbf{Device Nodes} are created automatically and
      then used by applications and operating system
      subsystems
      \item
      System administrators can control how \textbf{udev}
      operates and craft special \textbf{udev rules}.
   \end{itemize}

\end{frame}

\cprotect\note{

   \textbf{udev} actions include:
   \begin{itemize}
      \item Device naming
      \item Device file and symlink creating
      \item Setting file attributes
      \item Taking needed actions
   \end{itemize}


   When devices are added or removed from the system,
   \textbf{udev} receives a message from the kernel.  It
   then parses the rules .files in the
   \filelink{/etc/udev/rules.d} directory to see if any
   rules are there for the device added or removed.

   These rules are totally customizable and can specify
   device file names, device file creation, specify
   symlinks to be created, specify file attributes to be
   set for the device file (including user and group
   ownership), and even specify actions to be taken
   (programs to be executed).

}
\section{Device Nodes}

\begin{frame}
   {Device Nodes Connect Hardware with Software}

   \begin{figure}[H]
      \includegraphics[height=3.3in]{IMAGES/device_node_large}
      \caption{Device Nodes}
   \end{figure}

\end{frame}

\cprotect\note{

}

\begin{frame}
   {Device Nodes}
   \begin{itemize}
      \item
      \textbf{Device Nodes} are located in the \file{/dev} directory
      \item
      Character and Block devices have device nodes;
      Network devices do not
      \item
      Programs communicate with devices through these nodes
      using normal I/O methods
      \item
      One device driver may use multiple device nodes
      \item
      Device nodes can be created with:
      \begin{cmd}
$ sudo mknod [-m mode] /dev/name <type> <major> <minor>
      \end{cmd}
      e.g.,
      \begin{cmd}
$ sudo mknod -m 666 /dev/mycdrv c 254 1
      \end{cmd}
   \end{itemize}

\end{frame}

\cprotect\note {

   \begin{cmd}
$ ls -l /dev
   \end{cmd}
   \begin{response}
total 0
crw------- 1 coop audio      14,   4 Jul  9 01:54 audio
crw------- 1 root root       10,  62 Jul  9 01:54 autofs
lrwxrwxrwx 1 root root             4 Jul  9 01:54 cdrom -> scd0
lrwxrwxrwx 1 root root             4 Jul  9 01:54 cdrw -> scd0
crw------- 1 coop root        5,   1 Jul  9 06:54 console
....
lrwxrwxrwx 1 root root             4 Jul  9 01:54 dvd -> scd0
lrwxrwxrwx 1 root root             4 Jul  9 01:54 dvdwriter -> scd0
....
brw-r----- 1 root disk        8,   0 Jul  9 01:53 sda
brw-r----- 1 root disk        8,   1 Jul  9 01:53 sda1
brw-r----- 1 root disk        8,   2 Jul  9 06:54 sda2
....
brw-r----- 1 root disk        8,  16 Jul  9 01:53 sdb
brw-r----- 1 root disk        8,  17 Jul  9 01:53 sdb1
brw-r----- 1 root disk        8,  18 Jul  9 01:53 sdb2
....
crw-rw-rw- 1 root tty         5,   0 Jul  9 01:54 tty
crw-rw---- 1 root root        4,   0 Jul  9 14:54 tty0
crw------- 1 root root        4,   1 Jul  9 06:54 tty1
cr--r--r-- 1 root root        1,   9 Jul  9 01:53 urandom
....
crw-rw-rw- 1 root root        1,   5 Jul  9 01:54 zero
   \end{response}

}

\begin{frame}
   {udev Components}

   \begin{multicols}{2}
      \begin{itemize}
         \item
         \textbf{libudev} library

         Allows access to information about the devices
         \item
         \textbf{udevd} or \textbf{systemd-udevd} daemon

         Manages the \filelink{/dev} directory
         \item
         \textbf{udevadm}

         Utility for control and diagnostics
      \end{itemize}
      \begin{figure}[H]
         \includegraphics[height=3.3in]{IMAGES/udev_rule}
         \caption{udev Rules}
      \end{figure}
   \end{multicols}
\end{frame}

\cprotect\note{

   \textbf{udev} runs as a \textbf{daemon} (either
   \textbf{udevd} or \textbf{systemd-udevd}) and monitors a
   \textbf{netlink} socket.  When new devices are
   initialized or removed, the \textbf{uevent} kernel
   facility sends a message through the socket, which
   \textbf{udev} receives and takes appropriate action to
   create or remove device nodes of the right names and
   properties according to the rules.


   The three components of \textbf{udev} are:
   \begin{enumerate}
      \item
      The \textbf{libudev} library which allows access to
      information about the devices.
      \item
      The \textbf{udevd} daemon that manages the
      \filelink{/dev} directory
      \item
      The \textbf{udevadm} utility for control and diagnostics.
   \end{enumerate}

   The cleanest way to use \textbf{udev} is to have a pure
   system; the \filelink{/dev} directory is empty upon the
   initial kernel boot, and then is populated with device nodes
   as they are needed. When used this way, one must boot using
   an \textbf{initramfs} image, which may contain a set of
   preliminary device nodes as well as the the \textbf{udev}
   infrastructure.

}

\section{Rules}
\begin{frame}
   {udev Rule Files}
   \begin{itemize}
      \item Filename locations and format:
      \begin{kcode}
/etc/udev/rules.d/<rulename>.rules
/usr/lib/udev/rules.d/<rulename>.rules
      \end{kcode}
      \item Examples:
      \begin{kcode}
30-usb.rules
90-mycustom.rules
70-mouse.rules
60-persistent-storage.rules
      \end{kcode}
   \end{itemize}

\end{frame}

\cprotect\note{

   There are two separate parts defined on a single line:
   \begin{itemize}
      \item
      The first part consists of one or more match pairs
      denoted by \texttt{==} . These try to match a
      device's attributes and/or characteristics to some
      value.
      \item
      The second part consists of one or more assignment
      key-value pairs that assign a value to a name, such
      as a file name, group assignment, even file
      permissions, etc.
   \end{itemize}
   If no matching rule is found, it uses the default
   device node name and other attributes.

   \begin{cmd}
$ cat /etc/udev/rules.d/99-fitbit.rules
   \end{cmd}
   \begin{responsevar}{\footnotesize}
SUBSYSTEM=="usb", ATTR{idVendor}=="2687", ATTR{idProduct}=="fb01", SYMLINK+="fitbit", MODE="0666"
   \end{responsevar}
   \begin{cmd}
$ cat /etc/udev/rules.d/60-vboxdrv.rules
        \end{cmd}
   \begin{responsevar}{\footnotesize}
KERNEL=="vboxdrv", NAME="vboxdrv", OWNER="root", GROUP="vboxusers", MODE="0660"
KERNEL=="vboxdrvu", NAME="vboxdrvu", OWNER="root", GROUP="root", MODE="0666"
KERNEL=="vboxnetctl", NAME="vboxnetctl", OWNER="root", GROUP="vboxusers", MODE="0660"
SUBSYSTEM=="usb_device", ACTION=="add", RUN+="/usr/lib/virtualbox/VBoxCreateUSBNode.sh \
            $major $minor $attr{bDeviceClass}"
SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/usr/lib/virtualbox/VBoxCreateUSBNode.sh \
            $major $minor $attr{bDeviceClass}"
SUBSYSTEM=="usb_device", ACTION=="remove", RUN+="/usr/lib/virtualbox/VBoxCreateUSBNode.sh --remove \
            $major $minor"
SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="/usr/lib/virtualbox/VBoxCreateUSBNode.sh\
            --remove $major $minor"
   \end{responsevar}

}

\begin{frame}
   {Creating udev Rules}

   \begin{itemize}
      \item Format for rules:
      \begin{kcode}
<match><op>value [, ...] <assignment><op>value [, ... ]
      \end{kcode}

      \item Samples
      \begin{kcode}
KERNEL=="sdb", NAME="my-spare-disk"
KERNEL=="sdb", DRIVER=="usb-disk", SYMLINK+="sparedisk"
KERNEL=="sdb", RUN+="/usr/bin/my-program"
KERNEL=="sdb", MODE="0660", GROUP="mygroup"
      \end{kcode}
   \end{itemize}

\end{frame}

\cprotect\note{

   Rules files can be in three places, and if they have same name
   the priority order is:

   \begin{enumerate}
      \item \filelink{/etc/udev/rules.d}
      \item \filelink{/run/udev/rules.d}
      \item \filelink{/usr/lib/udev/rules.d}
   \end{enumerate}

   The format for a \textbf{udev} rule is simple. There
   are two separate parts defined on a single line.  The
   first part consists of one or more match pairs
   (denoted by double equal signs). These will match a
   device's attributes and/or characteristics to some
   value.  The second part consists of one or more
   assignment key-value pairs that assign a value to a
   name, such as a filename, group assignment, or even
   file permissions.

   If no matching rule is found, it uses the default
   device node name.

   See \verb?man udev? for explanation of the key values such as
   \verb?SYMLINK? and \verb?RUN?.

}


\begin{frame}
   {Some Examples of Rules Files}
   \begin{itemize}

      \item  \textbf{Fitbit} device:
      \begin{cmd}
$ cat /etc/udev/rules.d/99-fitbit.rules
   \end{cmd}
      \vspace{-5pt}
      \begin{response}
SUBSYSTEM=="usb", ATTR{idVendor}=="2687", ATTR{idProduct}=="fb01", \
                 SYMLINK+="fitbit", MODE="0666"
      \end{response}
      \item For creating crash dumps and fast kernel loading
      with \textbf{kdump/kexec}:
      \begin{cmd}
$ cat /usr/lib/udev/rules.d/98-kexec.rules
   \end{cmd}
      \vspace{-5pt}
      \begin{response}
SUBSYSTEM=="cpu", ACTION=="add", PROGRAM="/bin/systemctl \
                       try-restart kdump.service"
SUBSYSTEM=="cpu", ACTION=="remove", PROGRAM="/bin/systemctl \
                       try-restart kdump.service"
SUBSYSTEM=="memory", ACTION=="online", PROGRAM="/bin/systemctl \
                       try-restart kdump.service"
SUBSYSTEM=="memory", ACTION=="offline", PROGRAM="/bin/systemctl \
                     try-restart kdump.service"
      \end{response}
      \item For the \textbf{kvm} virtual machine hypervisor:
      \begin{cmd}
$ cat /usr/lib/udev/rules/.d/80-kvm.rules
        \end{cmd}
      \vspace{-5pt}
      \begin{response}
KERNEL=="kvm", GROUP="kvm", MODE="0666"
      \end{response}
   \end{itemize}
\end{frame}

\cprotect\note{ }

