LINUX 2
DEVICES
Devices are special type of files stored at
/dev dir.
[root@rhel7-server ~]# ls -l /dev
total 0
crw-rw----. 1
root video 10, 175 Nov 21 18:12 agpgart
drwxr-xr-x. 2
root root 300 Nov 21 18:37 block
lrwxrwxrwx. 1
root root 3 Nov 21 18:12 cdrom -> sr0
brw-rw----. 1
root floppy 2,0 Nov 21 18:12 fd0
prw-------. 1
root root 0 Nov 21 18:12 initctl
srw-rw-rw-. 1
root root 0 Nov 21 18:12 log
what are the c / d
/ l / b / p and s at the starting ?
c character
device
Only read characters from or
write characters to char devices.one character at a time.(kbd,mouse,printer)
d directory
Directories
l regular files
Regular files
b block device
data access can be done in fixed size
called chunks.
p pipe device
Named pipes are like char devices, Pipes and FIFOs (also known as named pipes) provide a unidirectional interprocess communication channel. A pipe has a read end and a write end. Data written to the write end of a pipe can be read from the read end of the pipe
s socket device
sockets are the uniform interface between the user process and the network protocol stacks in the kernel.
MAJOR &
MINOR DEVICE No.
Major device no
specifies the device class and the specific device driver for that class eg.
hard disk, input/output devices…etc
Minor device no’s
tells us that how many devices/no of members we have from that class, or it
tells what bus it connected to.
[root@rhel7-server
~]# ls -ltr /dev/sd*
brw-rw----. 1 root
disk 8, 32 Nov 21 18:12 /dev/sdc
brw-rw----. 1 root
disk 8, 64 Nov 21 18:12 /dev/sde
brw-rw----. 1 root
disk 8, 48 Nov 21 18:12 /dev/sdd
brw-rw----. 1 root
disk 8, 0 Nov 21 18:12 /dev/sda
brw-rw----. 1 root
disk 8, 5 Nov 21 18:12 /dev/sda5
brw-rw----. 1 root
disk 8, 4 Nov 21 18:12 /dev/sda4
brw-rw----. 1 root
disk 8, 3 Nov 21 18:12 /dev/sda3
brw-rw----. 1 root
disk 8, 1 Nov 21 18:12 /dev/sda1
brw-rw----. 1 root
disk 8, 2 Nov 21 18:12 /dev/sda2
brw-rw----. 1 root
disk 8, 16 Nov 21 18:37 /dev/sdb
brw-rw----. 1 root
disk 8, 17 Nov 21 18:40 /dev/sdb1
8 is major &
following is minor no.
[root@rhel7-server
~]# ls -l /dev |more
total 0
crw-rw----. 1 root
video 10, 175 Nov 21 18:12 agpgart
crw-------. 1 root
root 10, 235 Nov 21 18:12 autofs
drwxr-xr-x. 2 root
root 300 Nov 21 18:37 block
drwxr-xr-x. 2 root
root 160 Nov 21 18:12 bsg
crw-------. 1 root
root 10, 234 Nov 21 18:12
btrfs-control
drwxr-xr-x. 3 root
root 60 Nov 21 18:12 bus
lrwxrwxrwx. 1 root
root 3 Nov 21 18:12 cdrom ->
sr0
drwxr-xr-x. 2 root
root 3240 Nov 22 18:06 char
crw-------. 1 root
root 5, 1 Nov 21 18:12 console
lrwxrwxrwx. 1 root
root 11 Nov 21 18:12 core ->
/proc/kcore
drwxr-xr-x. 3 root
root 80 Nov 21 18:12 cpu
crw-------. 1 root
root 10, 61 Nov 21 18:12 cpu_dma_latency
[root@rhel7-server
~]# sar -d
Linux
3.10.0-121.el7.x86_64 (rhel7-server)
11/22/2016 _x86_64_ (1 CPU)
06:10:01 PM DEV
tps rd_sec/s wr_sec/s
avgrq-sz avgqu-sz await
svctm %util
06:20:01 PM dev2-0
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00
06:20:01 PM dev8-0
0.11 0.03 1.25 11.74
0.00 8.98 3.15
0.03
06:20:01 PM dev8-16
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00
06:20:01 PM dev8-32
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00
06:20:01 PM dev8-48
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00
06:20:01 PM dev8-64
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00
06:20:01 PM dev11-0
0.00 0.00 0.00
0.00 0.00
0.00 0.00 0.00
06:30:01 PM dev2-0
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00
06:30:01 PM dev8-0
0.15 7.47 1.30
58.79 0.00 11.75
2.60 0.04
So how to identify
what is dev8-0 and dev8-32 …and so on …
Check it in
# ls -ltr
/dev/sd*
SYSFS:
sysfs is a
mechanism for representing kernel objects, their attributes, and their
relationships with each other. It provides two components: a kernel programming
interface for exporting these items via sysfs, and a user interface to view and
manipulate these items that maps back to the kernel objects which they
represent. The table below shows the mapping between internel (kernel)
constructs and their external (userspace) sysfs mappings.
Internal External
Kernel Objects Directories
Object Attributes Regular
Files
Object
Relationships Symbolic Links
Courtesy:
[root@rhel7-server
~]# ls -l /sys
total 0
drwxr-xr-x. 2 root root 0 Nov 21 18:12 block
drwxr-xr-x. 24 root root 0 Nov 21 18:12 bus
drwxr-xr-x. 45 root root 0 Nov 21 18:12 class
drwxr-xr-x. 4 root root 0 Nov 21 18:12 dev
drwxr-xr-x. 12 root root 0 Nov 21 18:12 devices
drwxr-xr-x. 5 root root 0 Nov 21 18:12 firmware
drwxr-xr-x. 6 root root 0 Nov 21 18:12 fs
drwxr-xr-x. 2 root root 0 Nov 22 18:48 hypervisor
drwxr-xr-x. 8 root root 0 Nov 21 18:12 kernel
drwxr-xr-x. 164
root root 0 Nov 21 18:12 module
drwxr-xr-x. 2 root root 0 Nov 21 18:13 power
[root@rhel7-server
~]# ls -l /sys/class/scsi_disk/
total 0
lrwxrwxrwx. 1 root
root 0 Nov 21 18:12 2:0:0:0 ->
../../devices/pci0000:00/0000:00:10.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0
lrwxrwxrwx. 1 root
root 0 Nov 21 18:12 2:0:1:0 ->
../../devices/pci0000:00/0000:00:10.0/host2/target2:0:1/2:0:1:0/scsi_disk/2:0:1:0
lrwxrwxrwx. 1 root
root 0 Nov 21 18:12 2:0:2:0 ->
../../devices/pci0000:00/0000:00:10.0/host2/target2:0:2/2:0:2:0/scsi_disk/2:0:2:0
lrwxrwxrwx. 1 root
root 0 Nov 21 18:12 2:0:3:0 ->
../../devices/pci0000:00/0000:00:10.0/host2/target2:0:3/2:0:3:0/scsi_disk/2:0:3:0
lrwxrwxrwx. 1 root
root 0 Nov 21 18:12 2:0:4:0 ->
../../devices/pci0000:00/0000:00:10.0/host2/target2:0:4/2:0:4:0/scsi_disk/2:0:4:0
/dev/sd*
Sd scsi disk (small computer system interface)
[root@rhel7-server
~]# yum install -y lsscsi
[root@rhel7-server
~]# lsscsi
[1:0:0:0] cd/dvd
NECVMWar VMware IDE CDR10 1.00
/dev/sr0
[2:0:0:0] disk
VMware, VMware Virtual S 1.0 /dev/sda
[2:0:1:0] disk
VMware, VMware Virtual S 1.0 /dev/sdb
[2:0:2:0] disk
VMware, VMware Virtual S 1.0 /dev/sdc
[2:0:3:0] disk
VMware, VMware Virtual S 1.0 /dev/sdd
[2:0:4:0] disk
VMware, VMware Virtual S 1.0 /dev/sde
[root@rhel7-server
~]#
TERMINALS:
/dev/tty*
/dev/pts*
Terminal devices
are interface between a user process and I/O device. A process does need to be
attached to terminal.
USER SPACE &
KERNEL SPACE:
When a process is
created and its virtual memory is divided into user-space and a kernel-space ,
where user space region contains data, code, stack, heap of the process &
kernel-space space contains things such as the page table for the process,
kernel data structures and kernel code etc. To run kernel space code, control
must shift to kernel mode(using 0x80 software interrupt for system calls) &
kernel stack is basically shared among all processes currently executing in
kernel space.
The kernel runs in
the part of memory entitled to it. This part of memory cannot be accessed
directly by the processes of the normal users, while as the kernel can access
all parts of the memory. To access some part of the kernel, the user processes
have to use the predefined system calls i.e. open, read, write etc. Also, the C
library functions like printf call the system call write in turn.
The system calls
act as an interface between the user processes and the kernel processes. The
access rights are placed on the kernel space in order to stop the users from
messing up with the kernel, unknowingly.
So, when a system
call occurs, a software interrupt is sent to the kernel. The CPU may hand over
the control temporarily to the associated interrupt handler routine. The kernel
process which was halted by the interrupt resumes after the interrupt handler
routine finishes its job.
UDEV:
When a new device
is detected, the linux kernel sends a notification to user space process called
udev.
Behind the curtain…
The new device
characteristics are examined by user space process
Device file created
for newly detected device
Device has been
initialized
UDEV is default
device manager in 2.6 and above kernel versions. Udev depends upon proc and sys
file systems to be operational, and they must be mounted on /proc & /sys.
Ø While booting udev must start
before any device recognition.
Ø Udev runs in user space.
Ø Udev creates persistent device
names.
Ø Udev creates a dynamic /dev link
with device nodes for devices present in the system and allocate major/minor no
dynamically.
Ø Udev provides a user space API to
access the device info in the system.
Working of UDEV:
The udev daemon listens to the netlink
socket that kernel uses for communicating with user space applications. The
kernel will send a bunch of data through the netlink socket when a device is
added/removed from system. The udev daemon catches all data and will do the
rest.. device node creation, module loading……
KERNEL DEVICE
EVENT MANAGEMENT:
Ø When bootup is initialized, the
/dev directory is mounted in tmpfs
Ø After that, udev will copy the
static device nodes from /lib/udev/devices to /dev directory.
Ø The udev daemon then runs and
collects “uevents” from the kernel, for all the devices connected to system.
Ø The udev daemon will parse the
uevent data and will match it with rules specified in /etc/udev/rules.d
Ø Then the device nodes and
symbolic links for the device is created as specified in rules
Ø The udev daemon reads the rules
from /etc/udev/rules.d/*.rules and starts them in memory.
Ø Udev will receive an “inotify”
event, If any rules were changed, it will read the changes and update memory
accordingly.
DEVTMPFS:
Probing of devices at the time of booting.
If there is new device detected while booting, kernel may create device file if
required but also notifies udev about this device. Then udev performs device
initialization, then sym links are created in /dev to identify device.
Udev name the links
by interface type…then manufacturer and model info…then serial no and then
partition if available.
[root@rhel7-server
block]# ls -l /dev/disk/by-id
total 0
lrwxrwxrwx. 1 root
root 9 Nov 21 18:12 ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001
-> ../../sr0
UDEVADM:
Tool for udev.
Udevadm searches and explore the system devices as well as monitor “uevents”
received by kernel.
[root@rhel7-server
/]# udevadm info --query=all --name=/dev/sdb
P:
/devices/pci0000:00/0000:00:10.0/host2/target2:0:1/2:0:1:0/block/sdb
N: sdb
S:
disk/by-path/pci-0000:00:10.0-scsi-0:0:1:0
E:
DEVLINKS=/dev/disk/by-path/pci-0000:00:10.0-scsi-0:0:1:0
E: DEVNAME=/dev/sdb
E: DEVPATH=/devices/pci0000:00/0000:00:10.0/host2/target2:0:1/2:0:1:0/block/sdb
E: DEVTYPE=disk
E: ID_BUS=scsi
E:
ID_MODEL=VMware_Virtual_S
E:
ID_MODEL_ENC=VMware\x20Virtual\x20S
E:
ID_PART_TABLE_TYPE=dos
E:
ID_PATH=pci-0000:00:10.0-scsi-0:0:1:0
E: ID_PATH_TAG=pci-0000_00_10_0-scsi-0_0_1_0
E: ID_REVISION=1.0
E: ID_SCSI=1
E: ID_TYPE=disk
P= system device
path
N= device node
S= indicates sym
link to device node that udev placed in /dev/dir.
E= additional
device info extracted in the udev rules.
TO MONITOR UEVENTS:
[root@rhel7-server
/]# udevadm monitor
monitor will print
the received events for:
UDEV - the event
which udev sends out after rule processing
KERNEL - the kernel
uevent
^C
Inserted a USB
drive:
[root@rhel7-server
/]# udevadm monitor
monitor will print
the received events for:
UDEV - the event
which udev sends out after rule processing
KERNEL - the kernel
uevent
KERNEL[15184.205904]
add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1 (usb)
KERNEL[15184.227356]
add /devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0
(usb)
UDEV [15184.386208] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1 (usb)
KERNEL[15184.587538]
add /module/usb_storage (module)
03.0/usb1/1-1/1-1:1.0/host3/scsi_host/host3
(scsi_host)
KERNEL[15184.600101]
add /bus/usb/drivers/usb-storage
(drivers)
UDEV [15184.609651] add /bus/usb/drivers/usb-storage (drivers)
UDEV [15185.701128] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host3/target3:0:0/3:0:0:0/bsg/3:0:0:0
(bsg)
UDEV [15185.723150] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-
===================O/P
REMOVED================================
We can see there
are two types of heading of messages, one starts from KERNEL and other starts
from UDEV.
KERNEL= messages
coming from kernel.
UDEV= messages
send by udev to other programs.
If need to see only
kernel or udev events the apply --kernel/--udev like,
[root@rhel7-server
/]# udevadm monitor --udev
monitor will print
the received events for:
UDEV - the event
which udev sends out after rule processing
UDEV [15679.990551] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1 (usb)
UDEV [15680.006995] add /devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0
(usb)
UDEV [15680.013660] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host4
(scsi)
UDEV [15680.022107] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host4/scsi_host/host4
(scsi_host)
UDEV [15681.003255] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host4/target4:0:0
(scsi)
UDEV [15681.011468] add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0
(scsi)
UDEV [15681.024278] add
===================O/P
REMOVED================================
DBUS:
Dbus is a signaling
system that tells the programs about devices. Dbus reads the info from /dev dir
and relay that with signals to operator. If we plug something new, dbus signals
the GUI desktop manager about that device.
Basically it
organizes the system to store real time dynamic device info.
[root@rhel7-server
/]# ls -ltr /dev |more
total 0
drwxr-xr-x. 3 root
root 80 Nov 21 18:12 cpu
drwxr-xr-x. 2 root
root 0 Nov 21 18:12 pts
drwxrwxrwt. 2 root
root 40 Nov 21 18:12 mqueue
drwxr-xr-x. 2 root
root 60 Nov 21 18:12 raw
[root@rhel7-server
/]# ls -ltr /proc |more
total 0
lrwxrwxrwx. 1 root root 0 Nov 21 18:12 self
-> 2889
dr-xr-xr-x. 8 root root 0 Nov 21 18:12 1
dr-xr-xr-x. 1 root root 0 Nov 21 18:12 sys
dr-xr-xr-x. 8 root root 0 Nov 21 18:12 2
dr-xr-xr-x. 8 root root 0 Nov 21 18:12 99
dr-xr-xr-x. 8 root root 0 Nov 21 18:12 90
[root@rhel7-server
/]# ls -ltr /sys |more
total 0
drwxr-xr-x. 8 root root 0 Nov 21 18:12 kernel
drwxr-xr-x. 7 root root 0 Nov 21 18:12 fs
drwxr-xr-x. 5 root root 0 Nov 21 18:12 firmware
drwxr-xr-x. 170
root root 0 Nov 21 18:12 module
drwxr-xr-x. 12 root root 0 Nov 21 18:12 devices
drwxr-xr-x. 45 root root 0 Nov 21 18:12 class
[root@rhel7-server
/]# ls -ltr /sys/devices/ |more
total 0
drwxr-xr-x. 20 root
root 0 Nov 21 18:12 virtual
drwxr-xr-x. 6 root root 0 Nov 21 18:12 LNXSYSTM:00
drwxr-xr-x. 45 root
root 0 Nov 21 18:12 pci0000:00
drwxr-xr-x. 16 root
root 0 Nov 21 18:12 pnp0
drwxr-xr-x. 11 root
root 0 Nov 21 18:12 platform
drwxr-xr-x. 7 root root 0 Nov 21 18:12 system
when we attach a
device to system, it is automatically detected and there would be a window on
desktop containing that device contents (via desktop Manager).
When we plug a usb
device, kernel put it into “sysfs” file system (info about that device), then
udev populate /dev, then dbus notifies these standardized name of devices and
mount those devices automatically (if plug and play).
HOW UDEV WORKS:
Kernel detects the
attached device and sysfs is populated with device details. Udev receives
devices uevents from kerne.
Udev matches its
rules “/etc/udev/rules.d/*.rules” against various device attributes to identify
the device and create sym links accordingly, set permissions, rename devices or
run a script.
ACTIVITY CHECK:
Do this by plugging
and unplugging USB device.
[root@rhel7-server
/]# udevadm monitor --env
monitor will print
the received events for:
UDEV - the event
which udev sends out after rule processing
KERNEL - the kernel
uevent
KERNEL[17030.328941]
add
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1 (usb)
ACTION=add
BUSNUM=001
DEVNAME=/dev/bus/usb/001/004
DEVNUM=004
DEVPATH=/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1
DEVTYPE=usb_device
MAJOR=189
MINOR=3
PRODUCT=781/5567/127
SEQNUM=2406
SUBSYSTEM=usb
TYPE=0/0/0
===================O/P
REMOVED================================
===================FOLLOWING
AFTER REMOVAL OF USB=============
KERNEL[17061.718305]
remove /devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host5/target5:0:0/5:0:0:0/bsg/5:0:0:0
(bsg)
ACTION=remove
DEVNAME=/dev/bsg/5:0:0:0
DEVPATH=/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host5/target5:0:0/5:0:0:0/bsg/5:0:0:0
MAJOR=252
MINOR=6
SEQNUM=2419
SUBSYSTEM=bsg
KERNEL[17061.718424]
remove
/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host5/target5:0:0/5:0:0:0/scsi_generic/sg6
(scsi_generic)
ACTION=remove
DEVNAME=/dev/sg6
DEVPATH=/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host5/target5:0:0/5:0:0:0/scsi_generic/sg6
MAJOR=21
MINOR=6
SEQNUM=2420
SUBSYSTEM=scsi_generic
===================O/P
REMOVED================================
[root@rhel7-server
/]# udevadm info
Usage: udevadm info
OPTIONS
--query=<type> query device information:
name name of device node
symlink pointing to node
path sys device path
property the device properties
all all values
--path=<syspath> sys device path used for query or
attribute walk
--name=<name> node or symlink name used for
query or attribute walk
--root prepend dev directory to
path names
--attribute-walk print all key matches while walking
along the chain
of parent devices
--device-id-of-file=<file> print
major:minor of device containing this file
--export export key/value pairs
--export-prefix export the key name with a prefix
--export-db export the content of the udev
database
--cleanup-db cleanup the udev database
--help
[root@rhel7-server
/]#udevadm info /dev/sda1
[root@rhel7-server
/]# udevadm info --query=all --path=/sys/block/sdb
[root@rhel7-server
/]# cat /etc/udev/rules.d/98-kexec.rules
SUBSYSTEM=="cpu",
ACTION=="online", PROGRAM="/bin/systemctl try-restart
kdump.service"
SUBSYSTEM=="cpu",
ACTION=="offline", PROGRAM="/bin/systemctl try-restart
kdump.service"
SUBSYSTEM=="memory",
ACTION=="add", PROGRAM="/bin/systemctl try-restart
kdump.service"
SUBSYSTEM=="memory",
ACTION=="remove", PROGRAM="/bin/systemctl try-restart
kdump.service"
[root@rhel7-server
/]#
Thanks Sir. Every time i come back here, its like nostalgia. Always addictive.
ReplyDelete