**Description: **Candidates should be able to determine and configure fundamental system hardware.
Key Knowledge Areas:
Tools and utilities to list various hardware information (e.g. lsusb, lspci, etc.)
Tools and utilities to manipulate USB devices
Conceptual understanding of sysfs, udev, dbus
The following is a partial list of the used files, terms and utilities:
/sys/
/proc/
/dev/
modprobe
lsmod
lspci
lsusb
Linux treat every thing as a file. It includes programs , hardware and even processes which are running. These files are organized in directories and standardize for easier access and administration. Lets see how linux dealing with devices:
/proc
The /proc is a virtual dicretory which contains a illusionary filesystem called procfs. It does not exist on a disk. Instead, the kernel creates it in memory. It is used to provide information about the system (originally about processes).
Some of the more important files and directories are :
/proc/1: A directory with information about process number 1. Each process has a directory below /proc with the name being its process identification number.
**/proc/cpuinfo: **Information about the processor, such as its type, make, model, and performance.
The way /proc virtual directory organize processes get noticed by developers and they started to use /proc for both reading and writing information. So guess what, little by little /proc become a place which stored different kind of information. Processes information, current running Kernel information, hardware information, system information. Some one should do some thing to stop this messy place and that cause /sys introducing in kernel 2.5 .
/sys
/sys it is a virtual directory with illusionary sysfs file system, which is created when system boots up and get vanished when system restarts or goes off.
sysfs introduced to specifically store system information and its components (mostly attached and installed hardware). An as it was planned for that its seems more organized and more standardize than procfs.
root@ubuntu16-1:~# ls -l /sys/
total 0
drwxr-xr-x 2 root root 0 Oct 24 06:07 block
drwxr-xr-x 38 root root 0 Oct 24 06:07 bus
drwxr-xr-x 60 root root 0 Oct 24 06:07 class
drwxr-xr-x 4 root root 0 Oct 24 06:07 dev
drwxr-xr-x 14 root root 0 Oct 9 05:54 devices
drwxr-xr-x 5 root root 0 Oct 24 06:07 firmware
drwxr-xr-x 8 root root 0 Oct 9 05:54 fs
drwxr-xr-x 2 root root 0 Oct 24 06:07 hypervisor
drwxr-xr-x 11 root root 0 Oct 9 05:54 kernel
drwxr-xr-x 146 root root 0 Oct 24 06:07 module
drwxr-xr-x 2 root root 0 Oct 24 06:07 power
root@ubuntu16-1:~# ls /sys/devices/
breakpoint isa msr platform software tracepoint
cpu LNXSYSTM:00 pci0000:00 pnp0 system virtual
sysfs hasn't caused all the stuff move from /proc to /sys , they still exist in /proc but /sys gives us a better view of current data.
Linux kernel modules (LKMs) are pieces of code which can be loaded into the kernel much like a hot-swappable piece of hardware. they can be inserted into the kernel and activated without the system needing to be rebooted.
udev
The kernel is the central part of operating system to address the hardware. And to make sure that the hardware is available for the kernel udev plays an important role.
udev is a replacement for the Device File System (DevFS) starting with the Linux 2.6 kernel series. udev plays role in Loading Kernel Module, Creating Device Files and making sure every thing is the order we need it to be. Lets see how it works:
The linux kernel initiates the device loading and next sends out messages (uevents) to the udev daemon.
udev daemon catches the event and decide how to handle based on the attributes that it has received in the event. udev load required kernel module with necessary information using modprobe.
what is modprobe?
modprobe is an intelligent command for listing, inserting as well as removing modules from the kernel.( Will be explained )
3 . udev next reads its rules . udev allows us to ban devices based on their properties, like vendor ID and device ID, ... .
Default rules are in /lib/udev/rules.d
Custom rules are in /etc/udev/rules.d
Lets see it in action, we use and then attach a usb storage:
udev write device information to the /sys virtual directory. Also udev works as an Hardware Abstaraction Layer(HAL) and creates device file entries under /dev directory in a structured way.
What is HAL? In computers, a hardware abstraction layer (HAL) is a layer of programming that allows a computer OS to interact with a hardware device at a general or abstract level rather than at a detailed hardware level.
We have seen this information previously. Try udevadm info --attribute-walk --name=/dev/sda for your self. These device attributes can be used in udev rules.
/dev
This directory contains the device files for every hardware device attached to the system.
Device files are employed to provide the operating system and users an interface to the devices that they represent.
/dev exits from early beginning versions of linux and it was populated by devfs. (As we mentioned) devfs was a an obsolete and no longer available.
These days, it has been replaced by udev, a daemon that manages the contents of /dev in a temporary filesystem, **(**or by devtmpfs, which is a lightweight replacement for devfs that is used in some minimal systems).
Actually they are files and pointers to the under laying device hardware. Tryls -l to see that .
There are some common device names in .in linux World :
Name Device
cdrom CD drive
console Special entry for the currently used console.
cua* Serial ports
dsp* Devices for sampling and recording
fd* Entries for most kinds of floppy drives, the default is /dev/fd0, a floppy drive for 1.44 MB floppies.
hd[a-t][1-16] Standard support for IDE drives with maximum amount of partitions each.
ir* Infrared devices
isdn* Management of ISDN connections
js* Joystick(s)
lp* Printers
mem Memory
midi* midi player
mixer* and music Idealized model of a mixer (combines or adds signals)
modem Modem
mouse (also msmouse, logimouse, psmouse, input/mice, psaux) All kinds of mouses
null Bottomless garbage can
par* Entries for parallel port support
pty* Pseudo terminals
radio* For Radio Amateurs (HAMs).
ram* boot device
sd* SCSI disks with their partitions
sequencer For audio applications using the synthesizer features of the sound card (MIDI-device controller)
tty* Virtual consoles simulating vt100 terminals.
usb* USB card and scanner
video* For use with a graphics card supporting video.
with the special thanks of udev (as a Hardware Abstraction Layer) and the names it provides.
/sys vs /dev
The /sys filesystem (sysfs) contains files that provide information about devices: whether it's powered on, the vendor name and model, what bus the device is plugged into, etc. It's of interest to applications that manage devices.
The /dev filesystem contains files that allow programs to access the devices themselves: write data to a serial port, read a hard disk, etc. It's of interest to applications that access devices.
A metaphor is that /sys provides access to the packaging, while /dev provides access to the content of the box.
The reason for /dev existing independently of /sys is partly historical: /dev dates back to the dawn of Unix, while /sys is a much more recent invention. If Linux was designed today with no historical background, /dev/sda might be /sys/block/sda/content.
pesudo File Systems
'Pseudo-' means false, pretend. So "pseudo-filesystem" means a filesystem that doesn't have actual files – rather, it has virtual entries that the filesystem itself makes up on the spot.
/dev, /proc and /sys are virtual "pseudo-filesystems" (not existing on harddisk, but only in RAM – so they do not consume any harddisk space and are completely created on boot).
dbus
D-Bus is a message bus system, a simple way for applications to talk to one another. Beside all of dbus benefits it can read information form /dev folder and relate them with user desktop programs using signals. In fact dbus make a kind of middle layer which keeps programs a way from difficulties of dealing with /dev and /sys directories.
**Notice : **udev and dbus can work in all distributions because sysfs has made required information standardize.
From the administrative perspective there are some ls utilities ( lsusb, lspci , ... ) to show more information about the hardware which has been attached to our system. Lets take a quick look at them:
lsusb
The lsusb command allows you to display information about USB buses and devices that are attached to them.
root@ubuntu16-1:~# lsusb
Bus 001 Device 002: ID 0951:1625 Kingston Technology DataTraveler 101 II
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
lsusb has some options:
root@ubuntu16-1:~# lsusb --help
Usage: lsusb [options]...
List USB devices
-v, --verbose
Increase verbosity (show descriptors)
-s [[bus]:][devnum]
Show only devices with specified device and/or
bus numbers (in decimal)
-d vendor:[product]
Show only devices with the specified vendor and
product ID numbers (in hexadecimal)
-D device
Selects which device lsusb will examine
-t, --tree
Dump the physical USB device hierarchy as a tree
-V, --version
Show version of program
-h, --help
Show usage and help
We can also use the -v command-line option to display more verbose output:
-t tells lsusb to dump the physical USB device hierarchy as a tree. This overrides the v option.
root@ubuntu16-1:~# lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
|__ Port 1: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 12M
|__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/7p, 12M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M
|__ Port 1: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 480M
-Vor --version Print version information on standard output, then exit successfully.
root@ubuntu16-1:~# lsusb -V
lsusb (usbutils) 007
try usb-devices , it will give us more detailed info.
lscpu
lscpu reports information about the cpu and processing units. It does not have any further options or functionality.
root@ubuntu16-1:~# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 142
Model name: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
Stepping: 9
CPU MHz: 2904.002
BogoMIPS: 5808.00
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 4096K
NUMA node0 CPU(s): 0
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves arat
lshw
lshw is a general purpose utility, that reports detailed and brief information about multiple different hardware units such as cpu, memory, disk, usb controllers, network adapters etc. Lshw extracts the information from different /proc files.
Do remember that the lshw command executed by root (superuser):
root@ubuntu16-1:~# lshw --help
Hardware Lister (lshw) - B.02.17
usage: lshw [-format] [-options ...]
lshw -version
-version print program version (B.02.17)
format can be
-html output hardware tree as HTML
-xml output hardware tree as XML
-short output hardware paths
-businfo output bus information
options can be
-class CLASS only show a certain class of hardware
-C CLASS same as '-class CLASS'
-c CLASS same as '-class CLASS'
-disable TEST disable a test (like pci, isapnp, cpuid, etc. )
-enable TEST enable a test (like pci, isapnp, cpuid, etc. )
-quiet don't display status
-sanitize sanitize output (remove sensitive information like serial numbers, etc.)
-numeric output numeric IDs (for PCI, USB, etc.)
Lets try lshw -short :
root@ubuntu16-1:~# lshw -short
H/W path Device Class Description
========================================================
system VMware Virtual Platform
/0 bus 440BX Desktop Reference Platform
/0/0 memory 86KiB BIOS
/0/4 processor Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
/0/4/94 memory 16KiB L1 cache
/0/5 processor CPU
/0/5/95 memory 16KiB L1 cache
/0/6 processor CPU
/0/6/96 memory 16KiB L1 cache
/0/7 processor CPU
/0/7/97 memory 16KiB L1 cache
/0/8 processor CPU
/0/8/98 memory 16KiB L1 cache
/0/9 processor CPU
/0/9/99 memory 16KiB L1 cache
/0/a processor CPU
[output truncated]
lspci
lspci is a utility for displaying information about PCI buses in the system and devices connected to them.By default, it shows a brief list of devices.
root@ubuntu16-1:~# lspci --help
lspci: invalid option -- '-'
Usage: lspci [<switches>]
Basic display modes:
-mm Produce machine-readable output (single -m for an obsolete format)
-t Show bus tree
Display options:
-v Be verbose (-vv for very verbose)
-k Show kernel drivers handling each device
-x Show hex-dump of the standard part of the config space
-xxx Show hex-dump of the whole config space (dangerous; root only)
-xxxx Show hex-dump of the 4096-byte extended config space (root only)
-b Bus-centric view (addresses and IRQ's as seen by the bus)
-D Always show domain numbers
Resolving of device ID's to names:
-n Show numeric ID's
-nn Show both textual and numeric ID's (names & numbers)
-q Query the PCI ID database for unknown ID's via DNS
-qq As above, but re-query locally cached entries
-Q Query the PCI ID database for all ID's via DNS
Selection of devices:
-s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]] Show only devices in selected slots
-d [<vendor>]:[<device>][:<class>] Show only devices with specified ID's
Other options:
-i <file> Use specified ID database instead of /usr/share/misc/pci.ids.gz
-p <file> Look up kernel modules in a given file instead of default modules.pcimap
-M Enable `bus mapping' mode (dangerous; root only)
PCI access options:
-A <method> Use the specified PCI access method (see `-A help' for a list)
-O <par>=<val> Set PCI access parameter (see `-O help' for a list)
-G Enable PCI access debugging
-H <mode> Use direct hardware access (<mode> = 1 or 2)
-F <file> Read PCI configuration dump from a given file
The -t option will display the output in tree format with information about bus, and how devices are connected to those buses. The output will be only using the numerical ids:
lspci has a very helpful switch to know the name of the kernel module that will be handling the operations of a particular device. (this option will work only on Kernel 2.6 version and above):
lets try a tool in order to see whether these modules have been loaded.
lsmod
lsmod is a very simple program with no options.
root@ubuntu16-1:~# lsmod --help
Usage: lsmod
it nicely formats the contents of the file /proc/modules, which contains information about the status of all currently-loaded Linux Kernel Modules (LKMs).
There is nothing like Drivers in linux and as we said, udev is responsible for calling related module with required information using modprobe.
modprobe
modprobe intelligently adds or removes a module from the Linux kernel. For demonstration lets remove and add e1000 module which is for Ethernet car on my system: